File: moduleops_core.c

package info (click to toggle)
module-init-tools 3.4-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,208 kB
  • ctags: 900
  • sloc: sh: 7,980; ansic: 5,036; makefile: 204
file content (245 lines) | stat: -rw-r--r-- 7,045 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* Load the given section: NULL on error. */
static void *PERBIT(load_section)(ElfPERBIT(Ehdr) *hdr,
			    const char *secname,
			    unsigned long *size,
			    int conv)
{
	ElfPERBIT(Shdr) *sechdrs;
	unsigned int i;
	char *secnames;

	/* Grab section headers and strings so we can tell who is who */
	sechdrs = (void *)hdr + END(hdr->e_shoff, conv);
	secnames = (void *)hdr
		+ END(sechdrs[END(hdr->e_shstrndx, conv)].sh_offset, conv);

	/* Find the section they want */
	for (i = 1; i < END(hdr->e_shnum, conv); i++) {
		if (streq(secnames+END(sechdrs[i].sh_name, conv), secname)) {
			*size = END(sechdrs[i].sh_size, conv);
			return (void *)hdr + END(sechdrs[i].sh_offset, conv);
		}
	}
	*size = 0;
	return NULL;
}

static void PERBIT(load_symbols)(struct module *module)
{
	struct PERBIT(kernel_symbol) *ksyms;
	char *ksymstrings;
	unsigned long i, size;

	/* New-style: strings are in this section. */
	ksymstrings = PERBIT(load_section)(module->data, "__ksymtab_strings",
					   &size, module->conv);
	if (ksymstrings) {
		unsigned int i = 0;
		for (;;) {
			/* Skip any zero padding. */
			while (!ksymstrings[i])
				if (++i >= size)
					return;
			add_symbol(ksymstrings+i, module);
			i += strlen(ksymstrings+i);
		}
		/* GPL symbols too */
		ksymstrings = PERBIT(load_section)(module->data,
						   "__ksymtab_strings_gpl",
						   &size, module->conv);
		for (;;) {
			/* Skip any zero padding. */
			while (!ksymstrings[i])
				if (++i >= size)
					return;
			add_symbol(ksymstrings+i, module);
			i += strlen(ksymstrings+i);
		}
		return;
	}

	/* Old-style. */
	ksyms = PERBIT(load_section)(module->data, "__ksymtab", &size,
				     module->conv);
	for (i = 0; i < size / sizeof(struct PERBIT(kernel_symbol)); i++)
		add_symbol(ksyms[i].name, module);
	ksyms = PERBIT(load_section)(module->data, "__gpl_ksymtab", &size,
				     module->conv);
	for (i = 0; i < size / sizeof(struct PERBIT(kernel_symbol)); i++)
		add_symbol(ksyms[i].name, module);
}

static char *PERBIT(get_aliases)(struct module *module, unsigned long *size)
{
	return PERBIT(load_section)(module->data, ".modalias", size,
				    module->conv);
}

static char *PERBIT(get_modinfo)(struct module *module, unsigned long *size)
{
	return PERBIT(load_section)(module->data, ".modinfo", size,
				    module->conv);
}

#ifndef STT_REGISTER
#define STT_REGISTER    13              /* Global register reserved to app. */
#endif

/* Calculate the dependencies for this module */
static void PERBIT(calculate_deps)(struct module *module, int verbose)
{
	unsigned int i;
	unsigned long size;
	char *strings;
	ElfPERBIT(Sym) *syms;
	ElfPERBIT(Ehdr) *hdr;
	int handle_register_symbols;

	strings = PERBIT(load_section)(module->data, ".strtab", &size,
				       module->conv);
	syms = PERBIT(load_section)(module->data, ".symtab", &size,
				    module->conv);

	module->num_deps = 0;
	module->deps = NULL;

	if (!strings || !syms) {
		warn("Couldn't find symtab and strtab in module %s\n",
		     module->pathname);
		return;
	}

	hdr = module->data;
	handle_register_symbols = 0;
	if (END(hdr->e_machine, module->conv) == EM_SPARC ||
	    END(hdr->e_machine, module->conv) == EM_SPARCV9)
		handle_register_symbols = 1;

	for (i = 1; i < size / sizeof(syms[0]); i++) {
		if (END(syms[i].st_shndx, module->conv) == SHN_UNDEF) {
			/* Look for symbol */
			const char *name;
			struct module *owner;
			int weak;

			name = strings + END(syms[i].st_name, module->conv);

			/* Not really undefined: sparc gcc 3.3 creates
                           U references when you have global asm
                           variables, to avoid anyone else misusing
                           them. */
			if (handle_register_symbols
			    && (ELFPERBIT(ST_TYPE)(END(syms[i].st_info,
						       module->conv))
				== STT_REGISTER))
				continue;

			weak = (ELFPERBIT(ST_BIND)(END(syms[i].st_info,
						       module->conv))
				== STB_WEAK);
			owner = find_symbol(name, module->pathname, weak);
			if (owner) {
				if (verbose)
					printf("%s needs \"%s\": %s\n",
					       module->pathname, name,
					       owner->pathname);
				add_dep(module, owner);
			}
		}
	}
}

static void *PERBIT(deref_sym)(ElfPERBIT(Ehdr) *hdr, const char *name,
			       unsigned int *secsize,
			       int conv)
{
	unsigned int i;
	unsigned long size;
	char *strings;
	ElfPERBIT(Sym) *syms;
	ElfPERBIT(Shdr) *sechdrs;

	sechdrs = (void *)hdr + END(hdr->e_shoff, conv);
	strings = PERBIT(load_section)(hdr, ".strtab", &size, conv);
	syms = PERBIT(load_section)(hdr, ".symtab", &size, conv);

	/* Don't warn again: we already have above */
	if (!strings || !syms)
		return NULL;

	for (i = 0; i < size / sizeof(syms[0]); i++) {
		if (streq(strings + END(syms[i].st_name, conv), name)) {
			/* In BSS?  Happens for empty device tables on
			 * recent GCC versions. */
			if (END(sechdrs[END(syms[i].st_shndx, conv)].sh_type,
				conv) == SHT_NOBITS)
				return NULL;
			if (secsize)
				*secsize = END(syms[i].st_size, conv);
			return (void *)hdr
				+ END(sechdrs[END(syms[i].st_shndx, conv)]
				      .sh_offset, conv)
				+ END(syms[i].st_value, conv);
		}
	}
	return NULL;
}

/* FIXME: Check size, unless we end up using aliases anyway --RR */
static void PERBIT(fetch_tables)(struct module *module)
{
	module->pci_size = PERBIT(PCI_DEVICE_SIZE);
	module->pci_table = PERBIT(deref_sym)(module->data,
					"__mod_pci_device_table",
					NULL, module->conv);

	module->usb_size = PERBIT(USB_DEVICE_SIZE);
	module->usb_table = PERBIT(deref_sym)(module->data,
					"__mod_usb_device_table",
					NULL, module->conv);

	module->ccw_size = PERBIT(CCW_DEVICE_SIZE);
	module->ccw_table = PERBIT(deref_sym)(module->data,
					"__mod_ccw_device_table",
					NULL, module->conv);

	module->ieee1394_size = PERBIT(IEEE1394_DEVICE_SIZE);
	module->ieee1394_table = PERBIT(deref_sym)(module->data,
					"__mod_ieee1394_device_table",
					NULL, module->conv);

	module->pnp_size = PERBIT(PNP_DEVICE_SIZE);
	module->pnp_table = PERBIT(deref_sym)(module->data,
					"__mod_pnp_device_table",
					NULL, module->conv);

	module->pnp_card_size = PERBIT(PNP_CARD_DEVICE_SIZE);
	module->pnp_card_table = PERBIT(deref_sym)(module->data,
					"__mod_pnp_card_device_table",
					NULL, module->conv);
	module->pnp_card_offset = PERBIT(PNP_CARD_DEVICE_OFFSET);

	module->input_size = PERBIT(INPUT_DEVICE_SIZE);
	module->input_table = PERBIT(deref_sym)(module->data,
					"__mod_input_device_table",
					&module->input_table_size,
						module->conv);

	module->serio_size = PERBIT(SERIO_DEVICE_SIZE);
	module->serio_table = PERBIT(deref_sym)(module->data,
					"__mod_serio_device_table",
					NULL, module->conv);

	module->of_size = PERBIT(OF_DEVICE_SIZE);
	module->of_table = PERBIT(deref_sym)(module->data,
					"__mod_of_device_table",
					NULL, module->conv);
}

struct module_ops PERBIT(mod_ops) = {
	.load_symbols	= PERBIT(load_symbols),
	.calculate_deps	= PERBIT(calculate_deps),
	.fetch_tables	= PERBIT(fetch_tables),
	.get_aliases	= PERBIT(get_aliases),
	.get_modinfo	= PERBIT(get_modinfo),
};