History

fill got table for static linking

Ali Gholami Rudi committed 16 years ago

Commit d63ec6f1 parent b9aeac03 files · +54 −0Browse files

Unified · Split

3 files
  1. libtcc.c +9 −0
  2. tcc.h +1 −0
  3. tccelf.c +44 −0
libtcc.c+9−0
@@ -372,6 +372,15 @@ ST_FUNC void *section_ptr_add(Section *sec, unsigned long size) · 371 unmodified lines
372372 return sec->data + offset;
373373 }
374374
375+/* reserve at least 'size' bytes from section start */
376+ST_FUNC void section_reserve(Section *sec, unsigned long size)
377+{
378+ if (size > sec->data_allocated)
379+ section_realloc(sec, size);
380+ if (size > sec->data_offset)
381+ sec->data_offset = size;
382+}
383+
375384 /* return a reference to a section, and create it if it does not
376385 exists */
377386 ST_FUNC Section *find_section(TCCState *s1, const char *name)
tcc.h+1−0
@@ -900,6 +900,7 @@ ST_FUNC void add_char(CString *cstr, int c); · 899 unmodified lines
900900 ST_FUNC Section *new_section(TCCState *s1, const char *name, int sh_type, int sh_flags);
901901 ST_FUNC void section_realloc(Section *sec, unsigned long new_size);
902902 ST_FUNC void *section_ptr_add(Section *sec, unsigned long size);
903+ST_FUNC void section_reserve(Section *sec, unsigned long size);
903904 ST_FUNC Section *find_section(TCCState *s1, const char *name);
904905
905906 ST_FUNC void put_extern_sym2(Sym *sym, Section *section, unsigned long value, unsigned long size, int can_add_underscore);
tccelf.c+44−0
@@ -1365,6 +1365,48 @@ void patch_dynsym_undef(TCCState *s1, Section *s) · 1364 unmodified lines
13651365 #define EXTRA_RELITEMS 9
13661366 #endif
13671367
1368+ST_FUNC void fill_got_entry(TCCState *s1, ElfW_Rel *rel)
1369+{
1370+ int sym_index = ELFW(R_SYM) (rel->r_info);
1371+ ElfW(Sym) *sym = &((ElfW(Sym) *) symtab_section->data)[sym_index];
1372+ unsigned long offset;
1373+
1374+ if (sym_index >= s1->nb_got_offsets)
1375+ return;
1376+ offset = s1->got_offsets[sym_index];
1377+ section_reserve(s1->got, offset + PTR_SIZE);
1378+ /* only works for x86-64 */
1379+ put32(s1->got->data + offset, sym->st_value >> 32);
1380+ put32(s1->got->data + offset, sym->st_value & 0xffffffff);
1381+}
1382+
1383+ST_FUNC void fill_got(TCCState *s1)
1384+{
1385+ Section *s;
1386+ ElfW_Rel *rel, *rel_end;
1387+ int i;
1388+
1389+ for(i = 1; i < s1->nb_sections; i++) {
1390+ s = s1->sections[i];
1391+ if (s->sh_type != SHT_RELX)
1392+ continue;
1393+ /* no need to handle got relocations */
1394+ if (s->link != symtab_section)
1395+ continue;
1396+ rel_end = (ElfW_Rel *) (s->data + s->data_offset);
1397+ for(rel = (ElfW_Rel *) s->data; rel < rel_end; rel++) {
1398+ switch (ELFW(R_TYPE) (rel->r_info)) {
1399+ case R_X86_64_GOT32:
1400+ case R_X86_64_GOTPCREL:
1401+ case R_X86_64_PLT32:
1402+ fill_got_entry(s1, rel);
1403+ break;
1404+ }
1405+ }
1406+ }
1407+}
1408+
1409+
13681410 /* output an ELF file */
13691411 /* XXX: suppress unneeded sections */
13701412 static int elf_output_file(TCCState *s1, const char *filename)
@@ -1985,6 +2027,8 @@ static int elf_output_file(TCCState *s1, const char *filename) · 614 unmodified lines
19852027 else
19862028 ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */
19872029 }
2030+ if (file_type == TCC_OUTPUT_EXE && s1->static_link)
2031+ fill_got(s1);
19882032
19892033 /* write elf file */
19902034 if (file_type == TCC_OUTPUT_OBJ)

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