Unified · Split
| @@ -1081,8 +1081,11 @@ int tcc_output_file(TCCState *s1, const char *filename) · 1080 unmodified lines | ||
| 1081 | 1081 | file_type = s1->output_type; |
| 1082 | 1082 | s1->nb_errors = 0; |
| 1083 | 1083 | |
| 1084 | − if (file_type != TCC_OUTPUT_OBJ) | |
| 1084 | + if (file_type != TCC_OUTPUT_OBJ) { | |
| 1085 | + relocate_common_syms(); | |
| 1086 | + | |
| 1085 | 1087 | tcc_add_runtime(s1); |
| 1088 | + } | |
| 1086 | 1089 | |
| 1087 | 1090 | phdr = NULL; |
| 1088 | 1091 | section_order = NULL; |
| @@ -1093,8 +1096,6 @@ int tcc_output_file(TCCState *s1, const char *filename) · 4 unmodified lines | ||
| 1093 | 1096 | |
| 1094 | 1097 | if (file_type != TCC_OUTPUT_OBJ) { |
| 1095 | 1098 | |
| 1096 | − relocate_common_syms(); | |
| 1097 | − | |
| 1098 | 1099 | if (!s1->static_link) { |
| 1099 | 1100 | const char *name; |
| 1100 | 1101 | int sym_index, index; |
| @@ -1312,18 +1313,29 @@ int tcc_output_file(TCCState *s1, const char *filename) · 211 unmodified lines | ||
| 1312 | 1313 | file_offset = sizeof(Elf32_Ehdr) + phnum * sizeof(Elf32_Phdr); |
| 1313 | 1314 | if (phnum > 0) { |
| 1314 | 1315 | /* compute section to program header mapping */ |
| 1315 | − if (file_type == TCC_OUTPUT_DLL) | |
| 1316 | − addr = 0; | |
| 1317 | − else | |
| 1318 | − addr = ELF_START_ADDR; | |
| 1319 | − | |
| 1316 | + if (s1->has_text_addr) { | |
| 1317 | + int a_offset, p_offset; | |
| 1318 | + addr = s1->text_addr; | |
| 1319 | + /* we ensure that (addr % ELF_PAGE_SIZE) == file_offset % | |
| 1320 | + ELF_PAGE_SIZE */ | |
| 1321 | + a_offset = addr & (ELF_PAGE_SIZE - 1); | |
| 1322 | + p_offset = file_offset & (ELF_PAGE_SIZE - 1); | |
| 1323 | + if (a_offset < p_offset) | |
| 1324 | + a_offset += ELF_PAGE_SIZE; | |
| 1325 | + file_offset += (a_offset - p_offset); | |
| 1326 | + } else { | |
| 1327 | + if (file_type == TCC_OUTPUT_DLL) | |
| 1328 | + addr = 0; | |
| 1329 | + else | |
| 1330 | + addr = ELF_START_ADDR; | |
| 1331 | + /* compute address after headers */ | |
| 1332 | + addr += (file_offset & (ELF_PAGE_SIZE - 1)); | |
| 1333 | + } | |
| 1334 | + | |
| 1320 | 1335 | /* dynamic relocation table information, for .dynamic section */ |
| 1321 | 1336 | rel_size = 0; |
| 1322 | 1337 | rel_addr = 0; |
| 1323 | 1338 | |
| 1324 | − /* compute address after headers */ | |
| 1325 | − addr += (file_offset & (ELF_PAGE_SIZE - 1)); | |
| 1326 | − | |
| 1327 | 1339 | /* leave one program header for the program interpreter */ |
| 1328 | 1340 | ph = &phdr[0]; |
| 1329 | 1341 | if (interp) |
| @@ -1374,11 +1386,11 @@ int tcc_output_file(TCCState *s1, const char *filename) · 44 unmodified lines | ||
| 1374 | 1386 | section_order[sh_order_index++] = i; |
| 1375 | 1387 | |
| 1376 | 1388 | /* section matches: we align it and add its size */ |
| 1377 | − tmp = file_offset; | |
| 1378 | − file_offset = (file_offset + s->sh_addralign - 1) & | |
| 1389 | + tmp = addr; | |
| 1390 | + addr = (addr + s->sh_addralign - 1) & | |
| 1379 | 1391 | ~(s->sh_addralign - 1); |
| 1392 | + file_offset += addr - tmp; | |
| 1380 | 1393 | s->sh_offset = file_offset; |
| 1381 | − addr += file_offset - tmp; | |
| 1382 | 1394 | s->sh_addr = addr; |
| 1383 | 1395 | |
| 1384 | 1396 | /* update program header infos */ |