History

Relocation updates

x86_64-link.c: Check relocations R_X86_64_32/R_X86_64_32S. These relocations can become to large if not configured with '--disable-static' or '--config-pie' and code is run with -run -ltcc. This only happens on x86_64. tccdbg.c: 1) Add some .debug (dummy) sections when dwarf is used. These sections are otherwise not relocated correctly for R_DATA_32DW. 2) Fix stab problem when reloc is applied twice.

herman ten brugge committed 10 months ago

Commit 5a370d81 parent 64cf0b83 files · +34 −9Browse files

Unified · Split

3 files
  1. Makefile +1 −1
  2. tccdbg.c +24 −8
  3. x86_64-link.c +9 −0
Makefile+1−1
@@ -476,7 +476,7 @@ tcc_c$(EXESUF): $($T_FILES) · 475 unmodified lines
476476 sani-tes% : tcc_s$(EXESUF)
477477 @$(MAKE) --no-print-directory TCC_LOCAL=$(CURDIR)/$< tes$*
478478 tcc_s$(EXESUF): $($T_FILES)
479− $S$(CC) tcc.c -o $@ -fsanitize=address,undefined $(DEFINES) $(CFLAGS) $(LIBS)
479+ $S$(CC) tcc.c -o $@ -fsanitize=address,undefined $(DEFINES) $(CFLAGS) $(LDFLAGS) $(LIBS)
480480 # test the installed tcc instead
481481 test-install: $(TCCDEFS_H)
482482 @$(MAKE) -C tests TESTINSTALL=yes #_all
tccdbg.c+24−8
@@ -431,7 +431,7 @@ struct _tccdbg { · 430 unmodified lines
431431 #define FDE_ENCODING (DW_EH_PE_udata4 | DW_EH_PE_signed | DW_EH_PE_pcrel)
432432
433433 /* ------------------------------------------------------------------------- */
434−static void put_stabs(TCCState *s1, const char *str, int type, int other,
434+static int put_stabs(TCCState *s1, const char *str, int type, int other,
435435 int desc, unsigned long value);
436436
437437 ST_FUNC void tcc_debug_new(TCCState *s1)
@@ -449,6 +449,19 @@ ST_FUNC void tcc_debug_new(TCCState *s1) · 11 unmodified lines
449449 #endif
450450
451451 if (s1->dwarf) {
452+ int i;
453+ /* The sections below are just to make reloctions with
454+ R_DATA_32DW work correctly. See tccelf.c */
455+ static const char *const debug[] = {
456+ ".debug_macro",
457+ ".debug_loc",
458+ ".debug_ranges",
459+ ".debug_loclists",
460+ ".debug_rnglists",
461+ ".debug_str_offsets",
462+ ".debug_addr"
463+ };
464+
452465 s1->dwlo = s1->nb_sections;
453466 dwarf_info_section =
454467 new_section(s1, ".debug_info", SHT_PROGBITS, shf);
@@ -458,6 +471,8 @@ ST_FUNC void tcc_debug_new(TCCState *s1) · 3 unmodified lines
458471 new_section(s1, ".debug_line", SHT_PROGBITS, shf);
459472 dwarf_aranges_section =
460473 new_section(s1, ".debug_aranges", SHT_PROGBITS, shf);
474+ for (i = 0; i < sizeof(debug)/sizeof(debug[0]); i++)
475+ new_section(s1, debug[i], SHT_PROGBITS, 0)->sh_addralign = 1;
461476 shf |= SHF_MERGE | SHF_STRINGS;
462477 dwarf_str_section =
463478 new_section(s1, ".debug_str", SHT_PROGBITS, shf);
@@ -487,7 +502,7 @@ ST_FUNC void tcc_debug_new(TCCState *s1) · 23 unmodified lines
487502 }
488503
489504 /* put stab debug information */
490−static void put_stabs(TCCState *s1, const char *str, int type, int other, int desc,
505+static int put_stabs(TCCState *s1, const char *str, int type, int other, int desc,
491506 unsigned long value)
492507 {
493508 Stab_Sym *sym;
@@ -500,7 +515,7 @@ static void put_stabs(TCCState *s1, const char *str, int type, int other, int de · 6 unmodified lines
500515 && sym->n_value == value) {
501516 /* just update line_number in previous entry */
502517 sym->n_desc = desc;
503− return;
518+ return 0;
504519 }
505520
506521 sym = section_ptr_add(stab_section, sizeof(Stab_Sym));
@@ -513,16 +528,17 @@ static void put_stabs(TCCState *s1, const char *str, int type, int other, int de · 6 unmodified lines
513528 sym->n_other = other;
514529 sym->n_desc = desc;
515530 sym->n_value = value;
531+ return 1;
516532 }
517533
518534 static void put_stabs_r(TCCState *s1, const char *str, int type, int other, int desc,
519535 unsigned long value, Section *sec, int sym_index)
520536 {
521− put_elf_reloc(symtab_section, stab_section,
522− stab_section->data_offset + 8,
523− sizeof ((Stab_Sym*)0)->n_value == PTR_SIZE ? R_DATA_PTR : R_DATA_32,
524− sym_index);
525− put_stabs(s1, str, type, other, desc, value);
537+ if (put_stabs(s1, str, type, other, desc, value))
538+ put_elf_reloc(symtab_section, stab_section,
539+ stab_section->data_offset - 4,
540+ sizeof ((Stab_Sym*)0)->n_value == PTR_SIZE ? R_DATA_PTR : R_DATA_32,
541+ sym_index);
526542 }
527543
528544 static void put_stabn(TCCState *s1, int type, int other, int desc, int value)
x86_64-link.c+9−0
@@ -221,6 +221,15 @@ ST_FUNC void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, · 220 unmodified lines
221221 qrel->r_addend = (int)read32le(ptr) + val;
222222 qrel++;
223223 }
224+ /* ignore relocation check for stab section */
225+ if ((stab_section == NULL ||
226+ addr < stab_section->sh_addr ||
227+ addr >= (stab_section->sh_addr + stab_section->data_offset)) &&
228+ ((type == R_X86_64_32 &&
229+ (unsigned long long)val > 4294967295ULL) ||
230+ (type == R_X86_64_32S &&
231+ ((long long)val < -2147483648LL || (long long)val > 2147483647LL))))
232+ tcc_error_noabort("internal error: relocation %d failed", type);
224233 add32le(ptr, val);
225234 break;
226235

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t