Add option -e for setting ELF file's entry

This commit is contained in:
Ziyao 2022-04-18 14:26:15 +08:00
parent 0366924047
commit 39ea340a31
No known key found for this signature in database
GPG key ID: A32FA9E5C3BB744D
3 changed files with 12 additions and 2 deletions

View file

@ -1335,6 +1335,8 @@ static int tcc_set_linker(TCCState *s, const char *option)
s->symbolic = 1; s->symbolic = 1;
} else if (link_option(option, "nostdlib", &p)) { } else if (link_option(option, "nostdlib", &p)) {
s->nostdlib = 1; s->nostdlib = 1;
} else if (link_option(option, "e=", &p)) {
copy_linker_arg(&s->elf_entryname, p, 0);
} else if (link_option(option, "fini=", &p)) { } else if (link_option(option, "fini=", &p)) {
copy_linker_arg(&s->fini_symbol, p, 0); copy_linker_arg(&s->fini_symbol, p, 0);
ignoring = 1; ignoring = 1;

2
tcc.h
View file

@ -961,6 +961,8 @@ struct TCCState {
Section *verneed_section; Section *verneed_section;
#endif #endif
char *elf_entryname;
#ifdef TCC_IS_NATIVE #ifdef TCC_IS_NATIVE
const char *runtime_main; const char *runtime_main;
void **runtime_mem; void **runtime_mem;

View file

@ -2322,11 +2322,17 @@ static void tcc_output_elf(TCCState *s1, FILE *f, int phnum, ElfW(Phdr) *phdr,
default: default:
case TCC_OUTPUT_EXE: case TCC_OUTPUT_EXE:
ehdr.e_type = ET_EXEC; ehdr.e_type = ET_EXEC;
ehdr.e_entry = get_sym_addr(s1, "_start", 1, 0); ehdr.e_entry = get_sym_addr(s1,
s1->elf_entryname ?
s1->elf_entryname : "_start",
1, 0);
break; break;
case TCC_OUTPUT_DLL: case TCC_OUTPUT_DLL:
ehdr.e_type = ET_DYN; ehdr.e_type = ET_DYN;
ehdr.e_entry = text_section->sh_addr; /* XXX: is it correct ? */ ehdr.e_entry = s1->elf_entryname ?
get_sym_addr(s1,s1->elf_entryname,1,0) :
text_section->sh_addr;
/* XXX: is it correct ? */
break; break;
case TCC_OUTPUT_OBJ: case TCC_OUTPUT_OBJ:
ehdr.e_type = ET_REL; ehdr.e_type = ET_REL;