summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-08-28 21:38:10 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-08-28 21:38:10 +0000
commit3b80cac953b0627ba9b3337d3f9386678d436871 (patch)
tree64a3a0dbfd8539ab1db7adcbe70be6222793cab2
parent1ebdaccd6d25003202b41ca3f0f603f4bb97bb6e (diff)
downloadbusybox-w32-3b80cac953b0627ba9b3337d3f9386678d436871.tar.gz
busybox-w32-3b80cac953b0627ba9b3337d3f9386678d436871.tar.bz2
busybox-w32-3b80cac953b0627ba9b3337d3f9386678d436871.zip
insmod: mix xmalloc with xrealloc_vector more carefully
-rw-r--r--modutils/insmod.c89
1 files changed, 52 insertions, 37 deletions
diff --git a/modutils/insmod.c b/modutils/insmod.c
index 120feabf3..09e7d6811 100644
--- a/modutils/insmod.c
+++ b/modutils/insmod.c
@@ -1059,8 +1059,9 @@ arch_apply_relocation(struct obj_file *f,
1059 1059
1060 case R_68K_PC8: 1060 case R_68K_PC8:
1061 v -= dot; 1061 v -= dot;
1062 if ((ElfW(Sword))v > 0x7f || 1062 if ((ElfW(Sword))v > 0x7f
1063 (ElfW(Sword))v < -(ElfW(Sword))0x80) { 1063 || (ElfW(Sword))v < -(ElfW(Sword))0x80
1064 ) {
1064 ret = obj_reloc_overflow; 1065 ret = obj_reloc_overflow;
1065 } 1066 }
1066 *(char *)loc = v; 1067 *(char *)loc = v;
@@ -1068,8 +1069,9 @@ arch_apply_relocation(struct obj_file *f,
1068 1069
1069 case R_68K_PC16: 1070 case R_68K_PC16:
1070 v -= dot; 1071 v -= dot;
1071 if ((ElfW(Sword))v > 0x7fff || 1072 if ((ElfW(Sword))v > 0x7fff
1072 (ElfW(Sword))v < -(ElfW(Sword))0x8000) { 1073 || (ElfW(Sword))v < -(ElfW(Sword))0x8000
1074 ) {
1073 ret = obj_reloc_overflow; 1075 ret = obj_reloc_overflow;
1074 } 1076 }
1075 *(short *)loc = v; 1077 *(short *)loc = v;
@@ -1208,8 +1210,9 @@ arch_apply_relocation(struct obj_file *f,
1208 { 1210 {
1209 Elf32_Addr word; 1211 Elf32_Addr word;
1210 1212
1211 if ((Elf32_Sword)v > 0x7fff || 1213 if ((Elf32_Sword)v > 0x7fff
1212 (Elf32_Sword)v < -(Elf32_Sword)0x8000) { 1214 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1215 ) {
1213 ret = obj_reloc_overflow; 1216 ret = obj_reloc_overflow;
1214 } 1217 }
1215 1218
@@ -1238,8 +1241,9 @@ arch_apply_relocation(struct obj_file *f,
1238 Elf32_Addr word; 1241 Elf32_Addr word;
1239 1242
1240 v -= dot + 4; 1243 v -= dot + 4;
1241 if ((Elf32_Sword)v > 0x7fff || 1244 if ((Elf32_Sword)v > 0x7fff
1242 (Elf32_Sword)v < -(Elf32_Sword)0x8000) { 1245 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1246 ) {
1243 ret = obj_reloc_overflow; 1247 ret = obj_reloc_overflow;
1244 } 1248 }
1245 1249
@@ -1253,9 +1257,10 @@ arch_apply_relocation(struct obj_file *f,
1253 Elf32_Addr word, gp; 1257 Elf32_Addr word, gp;
1254 /* get _gp */ 1258 /* get _gp */
1255 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp")); 1259 gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
1256 v-=gp; 1260 v -= gp;
1257 if ((Elf32_Sword)v > 0x7fff || 1261 if ((Elf32_Sword)v > 0x7fff
1258 (Elf32_Sword)v < -(Elf32_Sword)0x8000) { 1262 || (Elf32_Sword)v < -(Elf32_Sword)0x8000
1263 ) {
1259 ret = obj_reloc_overflow; 1264 ret = obj_reloc_overflow;
1260 } 1265 }
1261 1266
@@ -2132,7 +2137,6 @@ obj_find_symbol(struct obj_file *f, const char *name)
2132 for (sym = f->symtab[hash]; sym; sym = sym->next) 2137 for (sym = f->symtab[hash]; sym; sym = sym->next)
2133 if (f->symbol_cmp(sym->name, name) == 0) 2138 if (f->symbol_cmp(sym->name, name) == 0)
2134 return sym; 2139 return sym;
2135
2136 return NULL; 2140 return NULL;
2137} 2141}
2138 2142
@@ -2141,12 +2145,10 @@ static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol
2141 if (sym) { 2145 if (sym) {
2142 if (sym->secidx >= SHN_LORESERVE) 2146 if (sym->secidx >= SHN_LORESERVE)
2143 return sym->value; 2147 return sym->value;
2144
2145 return sym->value + f->sections[sym->secidx]->header.sh_addr; 2148 return sym->value + f->sections[sym->secidx]->header.sh_addr;
2146 } else {
2147 /* As a special case, a NULL sym has value zero. */
2148 return 0;
2149 } 2149 }
2150 /* As a special case, a NULL sym has value zero. */
2151 return 0;
2150} 2152}
2151 2153
2152static struct obj_section *obj_find_section(struct obj_file *f, const char *name) 2154static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
@@ -2156,7 +2158,6 @@ static struct obj_section *obj_find_section(struct obj_file *f, const char *name
2156 for (i = 0; i < n; ++i) 2158 for (i = 0; i < n; ++i)
2157 if (strcmp(f->sections[i]->name, name) == 0) 2159 if (strcmp(f->sections[i]->name, name) == 0)
2158 return f->sections[i]; 2160 return f->sections[i];
2159
2160 return NULL; 2161 return NULL;
2161} 2162}
2162 2163
@@ -2167,9 +2168,11 @@ static int obj_load_order_prio(struct obj_section *a)
2167 af = a->header.sh_flags; 2168 af = a->header.sh_flags;
2168 2169
2169 ac = 0; 2170 ac = 0;
2170 if (a->name[0] != '.' || strlen(a->name) != 10 || 2171 if (a->name[0] != '.' || strlen(a->name) != 10
2171 strcmp(a->name + 5, ".init")) 2172 || strcmp(a->name + 5, ".init") != 0
2173 ) {
2172 ac |= 32; 2174 ac |= 32;
2175 }
2173 if (af & SHF_ALLOC) 2176 if (af & SHF_ALLOC)
2174 ac |= 16; 2177 ac |= 16;
2175 if (!(af & SHF_WRITE)) 2178 if (!(af & SHF_WRITE))
@@ -2856,18 +2859,19 @@ static int new_create_module_ksymtab(struct obj_file *f)
2856 /* We don't want to export symbols residing in sections that 2859 /* We don't want to export symbols residing in sections that
2857 aren't loaded. There are a number of these created so that 2860 aren't loaded. There are a number of these created so that
2858 we make sure certain module options don't appear twice. */ 2861 we make sure certain module options don't appear twice. */
2859 2862 i = f->header.e_shnum;
2860 loaded = alloca(sizeof(int) * (i = f->header.e_shnum)); 2863 loaded = alloca(sizeof(int) * i);
2861 while (--i >= 0) 2864 while (--i >= 0)
2862 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0; 2865 loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
2863 2866
2864 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) { 2867 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
2865 struct obj_symbol *sym; 2868 struct obj_symbol *sym;
2866 for (sym = f->symtab[i]; sym; sym = sym->next) 2869 for (sym = f->symtab[i]; sym; sym = sym->next) {
2867 if (ELF_ST_BIND(sym->info) != STB_LOCAL 2870 if (ELF_ST_BIND(sym->info) != STB_LOCAL
2868 && sym->secidx <= SHN_HIRESERVE 2871 && sym->secidx <= SHN_HIRESERVE
2869 && (sym->secidx >= SHN_LORESERVE 2872 && (sym->secidx >= SHN_LORESERVE
2870 || loaded[sym->secidx])) { 2873 || loaded[sym->secidx])
2874 ) {
2871 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p; 2875 ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
2872 2876
2873 obj_symbol_patch(f, sec->idx, ofs, sym); 2877 obj_symbol_patch(f, sec->idx, ofs, sym);
@@ -2876,6 +2880,7 @@ static int new_create_module_ksymtab(struct obj_file *f)
2876 2880
2877 nsyms++; 2881 nsyms++;
2878 } 2882 }
2883 }
2879 } 2884 }
2880 2885
2881 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p); 2886 obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
@@ -2934,9 +2939,11 @@ new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
2934 } 2939 }
2935 sec = obj_find_section(f, ".data.init"); 2940 sec = obj_find_section(f, ".data.init");
2936 if (sec) { 2941 if (sec) {
2937 if (!module->runsize || 2942 if (!module->runsize
2938 module->runsize > sec->header.sh_addr - m_addr) 2943 || module->runsize > sec->header.sh_addr - m_addr
2944 ) {
2939 module->runsize = sec->header.sh_addr - m_addr; 2945 module->runsize = sec->header.sh_addr - m_addr;
2946 }
2940 } 2947 }
2941 sec = obj_find_section(f, ARCHDATA_SEC_NAME); 2948 sec = obj_find_section(f, ARCHDATA_SEC_NAME);
2942 if (sec && sec->header.sh_size) { 2949 if (sec && sec->header.sh_size) {
@@ -3219,8 +3226,8 @@ static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3219#if SHT_RELM == SHT_RELA 3226#if SHT_RELM == SHT_RELA
3220#if defined(__alpha__) && defined(AXP_BROKEN_GAS) 3227#if defined(__alpha__) && defined(AXP_BROKEN_GAS)
3221 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */ 3228 /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
3222 if (!extsym || !extsym->st_name || 3229 if (!extsym || !extsym->st_name
3223 ELF_ST_BIND(extsym->st_info) != STB_LOCAL) 3230 || ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
3224#endif 3231#endif
3225 value += rel->r_addend; 3232 value += rel->r_addend;
3226#endif 3233#endif
@@ -3326,16 +3333,17 @@ static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM)
3326 } 3333 }
3327 3334
3328 if (f->header.e_ident[EI_MAG0] != ELFMAG0 3335 if (f->header.e_ident[EI_MAG0] != ELFMAG0
3329 || f->header.e_ident[EI_MAG1] != ELFMAG1 3336 || f->header.e_ident[EI_MAG1] != ELFMAG1
3330 || f->header.e_ident[EI_MAG2] != ELFMAG2 3337 || f->header.e_ident[EI_MAG2] != ELFMAG2
3331 || f->header.e_ident[EI_MAG3] != ELFMAG3) { 3338 || f->header.e_ident[EI_MAG3] != ELFMAG3
3339 ) {
3332 bb_error_msg_and_die("not an ELF file"); 3340 bb_error_msg_and_die("not an ELF file");
3333 } 3341 }
3334 if (f->header.e_ident[EI_CLASS] != ELFCLASSM 3342 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3335 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN 3343 || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB)
3336 ? ELFDATA2MSB : ELFDATA2LSB) 3344 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3337 || f->header.e_ident[EI_VERSION] != EV_CURRENT 3345 || !MATCH_MACHINE(f->header.e_machine)
3338 || !MATCH_MACHINE(f->header.e_machine)) { 3346 ) {
3339 bb_error_msg_and_die("ELF file not for this architecture"); 3347 bb_error_msg_and_die("ELF file not for this architecture");
3340 } 3348 }
3341 if (f->header.e_type != ET_REL) { 3349 if (f->header.e_type != ET_REL) {
@@ -3351,7 +3359,10 @@ static struct obj_file *obj_load(FILE *fp, int loadprogbits UNUSED_PARAM)
3351 } 3359 }
3352 3360
3353 shnum = f->header.e_shnum; 3361 shnum = f->header.e_shnum;
3354 f->sections = xzalloc(sizeof(struct obj_section *) * shnum); 3362 /* Growth of ->sections vector will be done by
3363 * xrealloc_vector(..., 2, ...), therefore we must allocate
3364 * at least 2^2 = 4 extra elements here. */
3365 f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4));
3355 3366
3356 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum); 3367 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3357 fseek(fp, f->header.e_shoff, SEEK_SET); 3368 fseek(fp, f->header.e_shoff, SEEK_SET);
@@ -3855,16 +3866,20 @@ static void print_load_map(struct obj_file *f)
3855 for (nsyms = i = 0; i < HASH_BUCKETS; ++i) 3866 for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
3856 for (sym = f->symtab[i]; sym; sym = sym->next) 3867 for (sym = f->symtab[i]; sym; sym = sym->next)
3857 if (sym->secidx <= SHN_HIRESERVE 3868 if (sym->secidx <= SHN_HIRESERVE
3858 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])) 3869 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
3870 ) {
3859 ++nsyms; 3871 ++nsyms;
3872 }
3860 3873
3861 all = alloca(nsyms * sizeof(struct obj_symbol *)); 3874 all = alloca(nsyms * sizeof(struct obj_symbol *));
3862 3875
3863 for (i = 0, p = all; i < HASH_BUCKETS; ++i) 3876 for (i = 0, p = all; i < HASH_BUCKETS; ++i)
3864 for (sym = f->symtab[i]; sym; sym = sym->next) 3877 for (sym = f->symtab[i]; sym; sym = sym->next)
3865 if (sym->secidx <= SHN_HIRESERVE 3878 if (sym->secidx <= SHN_HIRESERVE
3866 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])) 3879 && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
3880 ) {
3867 *p++ = sym; 3881 *p++ = sym;
3882 }
3868 3883
3869 /* And list them. */ 3884 /* And list them. */
3870 printf("\nSymbols:\n"); 3885 printf("\nSymbols:\n");