📄 outelf.c
字号:
}
s = NULL;
for (i = 0; i < nsects; i++)
if (segto == sects[i]->index) {
s = sects[i];
break;
}
if (!s) {
int tempint; /* ignored */
if (segto != elf_section_names(".text", 2, &tempint))
error(ERR_PANIC, "strange segment conditions in ELF driver");
else {
s = sects[nsects - 1];
i = nsects - 1;
}
}
/* again some stabs debugging stuff */
if (of_elf.current_dfmt) {
sinfo.offset = s->len;
sinfo.section = i;
sinfo.name = s->name;
of_elf.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
}
/* end of debugging stuff */
if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
error(ERR_WARNING, "attempt to initialise memory in"
" BSS section `%s': ignored", s->name);
if (type == OUT_REL2ADR)
realbytes = 2;
else if (type == OUT_REL4ADR)
realbytes = 4;
s->len += realbytes;
return;
}
if (type == OUT_RESERVE) {
if (s->type == SHT_PROGBITS) {
error(ERR_WARNING, "uninitialised space declared in"
" non-BSS section `%s': zeroing", s->name);
elf_sect_write(s, NULL, realbytes);
} else
s->len += realbytes;
} else if (type == OUT_RAWDATA) {
if (segment != NO_SEG)
error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
elf_sect_write(s, data, realbytes);
} else if (type == OUT_ADDRESS) {
int gnu16 = 0;
addr = *(long *)data;
if (segment != NO_SEG) {
if (segment % 2) {
error(ERR_NONFATAL, "ELF format does not support"
" segment base references");
} else {
if (wrt == NO_SEG) {
if (realbytes == 2) {
gnu16 = 1;
elf_add_reloc(s, segment, R_386_16);
} else {
elf_add_reloc(s, segment, R_386_32);
}
} else if (wrt == elf_gotpc_sect + 1) {
/*
* The user will supply GOT relative to $$. ELF
* will let us have GOT relative to $. So we
* need to fix up the data item by $-$$.
*/
addr += s->len;
elf_add_reloc(s, segment, R_386_GOTPC);
} else if (wrt == elf_gotoff_sect + 1) {
elf_add_reloc(s, segment, R_386_GOTOFF);
} else if (wrt == elf_got_sect + 1) {
addr = elf_add_gsym_reloc(s, segment, addr,
R_386_GOT32, TRUE);
} else if (wrt == elf_sym_sect + 1) {
if (realbytes == 2) {
gnu16 = 1;
addr = elf_add_gsym_reloc(s, segment, addr,
R_386_16, FALSE);
} else {
addr = elf_add_gsym_reloc(s, segment, addr,
R_386_32, FALSE);
}
} else if (wrt == elf_plt_sect + 1) {
error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
"relative PLT references");
} else {
error(ERR_NONFATAL, "ELF format does not support this"
" use of WRT");
wrt = NO_SEG; /* we can at least _try_ to continue */
}
}
}
p = mydata;
if (gnu16) {
error(ERR_WARNING | ERR_WARN_GNUELF,
"16-bit relocations in ELF is a GNU extension");
WRITESHORT(p, addr);
} else {
if (realbytes != 4 && segment != NO_SEG) {
error(ERR_NONFATAL,
"Unsupported non-32-bit ELF relocation");
}
WRITELONG(p, addr);
}
elf_sect_write(s, mydata, realbytes);
} else if (type == OUT_REL2ADR) {
if (segment == segto)
error(ERR_PANIC, "intra-segment OUT_REL2ADR");
if (segment != NO_SEG && segment % 2) {
error(ERR_NONFATAL, "ELF format does not support"
" segment base references");
} else {
if (wrt == NO_SEG) {
error(ERR_WARNING | ERR_WARN_GNUELF,
"16-bit relocations in ELF is a GNU extension");
elf_add_reloc(s, segment, R_386_PC16);
} else {
error(ERR_NONFATAL,
"Unsupported non-32-bit ELF relocation");
}
}
p = mydata;
WRITESHORT(p, *(long *)data - realbytes);
elf_sect_write(s, mydata, 2L);
} else if (type == OUT_REL4ADR) {
if (segment == segto)
error(ERR_PANIC, "intra-segment OUT_REL4ADR");
if (segment != NO_SEG && segment % 2) {
error(ERR_NONFATAL, "ELF format does not support"
" segment base references");
} else {
if (wrt == NO_SEG) {
elf_add_reloc(s, segment, R_386_PC32);
} else if (wrt == elf_plt_sect + 1) {
elf_add_reloc(s, segment, R_386_PLT32);
} else if (wrt == elf_gotpc_sect + 1 ||
wrt == elf_gotoff_sect + 1 ||
wrt == elf_got_sect + 1) {
error(ERR_NONFATAL, "ELF format cannot produce PC-"
"relative GOT references");
} else {
error(ERR_NONFATAL, "ELF format does not support this"
" use of WRT");
wrt = NO_SEG; /* we can at least _try_ to continue */
}
}
p = mydata;
WRITELONG(p, *(long *)data - realbytes);
elf_sect_write(s, mydata, 4L);
}
}
static void elf_write(void)
{
int nsections, align;
int scount;
char *p;
int commlen;
char comment[64];
int i;
struct SAA *symtab;
long symtablen, symtablocal;
/*
* Work out how many sections we will have. We have SHN_UNDEF,
* then the flexible user sections, then the four fixed
* sections `.comment', `.shstrtab', `.symtab' and `.strtab',
* then optionally relocation sections for the user sections.
*/
if (of_elf.current_dfmt == &df_stabs)
nsections = 8;
else
nsections = 5; /* SHN_UNDEF and the fixed ones */
add_sectname("", ".comment");
add_sectname("", ".shstrtab");
add_sectname("", ".symtab");
add_sectname("", ".strtab");
for (i = 0; i < nsects; i++) {
nsections++; /* for the section itself */
if (sects[i]->head) {
nsections++; /* for its relocations */
add_sectname(".rel", sects[i]->name);
}
}
if (of_elf.current_dfmt == &df_stabs) {
/* in case the debug information is wanted, just add these three sections... */
add_sectname("", ".stab");
add_sectname("", ".stabstr");
add_sectname(".rel", ".stab");
}
/*
* Do the comment.
*/
*comment = '\0';
commlen =
2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
/*
* Output the ELF header.
*/
fwrite("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp);
fwriteshort(1, elffp); /* ET_REL relocatable file */
fwriteshort(3, elffp); /* EM_386 processor ID */
fwritelong(1L, elffp); /* EV_CURRENT file format version */
fwritelong(0L, elffp); /* no entry point */
fwritelong(0L, elffp); /* no program header table */
fwritelong(0x40L, elffp); /* section headers straight after
* ELF header plus alignment */
fwritelong(0L, elffp); /* 386 defines no special flags */
fwriteshort(0x34, elffp); /* size of ELF header */
fwriteshort(0, elffp); /* no program header table, again */
fwriteshort(0, elffp); /* still no program header table */
fwriteshort(0x28, elffp); /* size of section header */
fwriteshort(nsections, elffp); /* number of sections */
fwriteshort(nsects + 2, elffp); /* string table section index for
* section header table */
fwritelong(0L, elffp); /* align to 0x40 bytes */
fwritelong(0L, elffp);
fwritelong(0L, elffp);
/*
* Build the symbol table and relocation tables.
*/
symtab = elf_build_symtab(&symtablen, &symtablocal);
for (i = 0; i < nsects; i++)
if (sects[i]->head)
sects[i]->rel = elf_build_reltab(§s[i]->rellen,
sects[i]->head);
/*
* Now output the section header table.
*/
elf_foffs = 0x40 + 0x28 * nsections;
align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
elf_foffs += align;
elf_nsect = 0;
elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
elf_section_header(0, 0, 0, NULL, FALSE, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
scount = 1; /* needed for the stabs debugging to track the symtable section */
p = shstrtab + 1;
for (i = 0; i < nsects; i++) {
elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
(sects[i]->type == SHT_PROGBITS ?
sects[i]->data : NULL), TRUE,
sects[i]->len, 0, 0, sects[i]->align, 0);
p += strlen(p) + 1;
scount++; /* dito */
}
elf_section_header(p - shstrtab, 1, 0, comment, FALSE, (long)commlen, 0, 0, 1, 0); /* .comment */
scount++; /* dito */
p += strlen(p) + 1;
elf_section_header(p - shstrtab, 3, 0, shstrtab, FALSE, (long)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
scount++; /* dito */
p += strlen(p) + 1;
elf_section_header(p - shstrtab, 2, 0, symtab, TRUE, symtablen, nsects + 4, symtablocal, 4, 16); /* .symtab */
symtabsection = scount; /* now we got the symtab section index in the ELF file */
p += strlen(p) + 1;
elf_section_header(p - shstrtab, 3, 0, strs, TRUE, strslen, 0, 0, 1, 0); /* .strtab */
for (i = 0; i < nsects; i++)
if (sects[i]->head) {
p += strlen(p) + 1;
elf_section_header(p - shstrtab, 9, 0, sects[i]->rel, TRUE,
sects[i]->rellen, nsects + 3, i + 1, 4, 8);
}
if (of_elf.current_dfmt == &df_stabs) {
/* for debugging information, create the last three sections
which are the .stab , .stabstr and .rel.stab sections respectively */
/* this function call creates the stab sections in memory */
stabs_generate();
if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
p += strlen(p) + 1;
elf_section_header(p - shstrtab, 1, 0, stabbuf, 0, stablen,
nsections - 2, 0, 4, 12);
p += strlen(p) + 1;
elf_section_header(p - shstrtab, 3, 0, stabstrbuf, 0,
stabstrlen, 0, 0, 4, 0);
p += strlen(p) + 1;
/* link -> symtable info -> section to refer to */
elf_section_header(p - shstrtab, 9, 0, stabrelbuf, 0,
stabrellen, symtabsection, nsections - 3, 4,
8);
}
}
fwrite(align_str, align, 1, elffp);
/*
* Now output the sections.
*/
elf_write_sections();
nasm_free(elf_sects);
saa_free(symtab);
}
static struct SAA *elf_build_symtab(long *len, long *local)
{
struct SAA *s = saa_init(1L);
struct Symbol *sym;
unsigned char entry[16], *p;
int i;
*len = *local = 0;
/*
* First, an all-zeros entry, required by the ELF spec.
*/
saa_wbytes(s, NULL, 16L); /* null symbol table entry */
*len += 16;
(*local)++;
/*
* Next, an entry for the file name.
*/
p = entry;
WRITELONG(p, 1); /* we know it's 1st thing in strtab */
WRITELONG(p, 0); /* no value */
WRITELONG(p, 0); /* no size either */
WRITESHORT(p, 4); /* type FILE */
WRITESHORT(p, SHN_ABS);
saa_wbytes(s, entry, 16L);
*len += 16;
(*local)++;
/*
* Now some standard symbols defining the segments, for relocation
* purposes.
*/
for (i = 1; i <= nsects + 1; i++) {
p = entry;
WRITELONG(p, 0); /* no symbol name */
WRITELONG(p, 0); /* offset zero */
WRITELONG(p, 0); /* size zero */
WRITESHORT(p, 3); /* local section-type thing */
WRITESHORT(p, (i == 1 ? SHN_ABS : i - 1)); /* the section id */
saa_wbytes(s, entry, 16L);
*len += 16;
(*local)++;
}
/*
* Now the other local symbols.
*/
saa_rewind(syms);
while ((sym = saa_rstruct(syms))) {
if (sym->type & SYM_GLOBAL)
continue;
p = entry;
WRITELONG(p, sym->strpos);
WRITELONG(p, sym->value);
WRITELONG(p, sym->size);
WRITESHORT(p, sym->type); /* local non-typed thing */
WRITESHORT(p, sym->section);
saa_wbytes(s, entry, 16L);
*len += 16;
(*local)++;
}
/*
* Now the global symbols.
*/
saa_rewind(syms);
while ((sym = saa_rstruct(syms))) {
if (!(sym->type & SYM_GLOBAL))
continue;
p = entry;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -