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");
/* 计算有几个 section */
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; /* elf_foffs 存放的是 section 的内容存放的其实位置。 */
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; /* 存放 section name 的字符串 */
/* 处理 segment 的 section */
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 */
}
/* commnet */
elf_section_header(p - shstrtab, 1, 0, comment, FALSE, (long)commlen, 0, 0, 1, 0); /* .comment */
scount++; /* dito */
p += strlen(p) + 1;
/* 存放 section 名字的 section */
elf_section_header(p - shstrtab, 3, 0, shstrtab, FALSE, (long)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
scount++; /* dito */
p += strlen(p) + 1;
/* 存放 符号表实例的 section */
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;
/* 存放字符串的 section */
elf_section_header(p - shstrtab, 3, 0, strs, TRUE, strslen, 0, 0, 1, 0); /* .strtab */
/* 处理重定位 section */
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;
WRITELONG(p, sym->strpos);
WRITELONG(p, sym->value);
WRITELONG(p, sym->size);
WRITESHORT(p, sym->type); /* global non-typed thing */
WRITESHORT(p, sym->section);
saa_wbytes(s, entry, 16L);
*len += 16;
}
return s;
}
static struct SAA *elf_build_reltab(long *len, struct Reloc *r)
{
struct SAA *s;
unsigned char *p, entry[8];
if (!r)
return NULL;
s = saa_init(1L);
*len = 0;
while (r) {
long sym = r->symbol;
if (sym >= GLOBAL_TEMP_BASE) /* extern func3
* call func3
*/
sym += -GLOBAL_TEMP_BASE + (nsects + 3) + nlocals;
p = entry;
WRITELONG(p, r->address);
WRITELONG(p, (sym << 8) + r->type);
saa_wbytes(s, entry, 8L);
*len += 8;
r = r->next;
}
return s;
}
/* 输入 section header */
static void elf_section_header(int name, int type, int flags, void *data, int is_saa, long datalen, int link, int info, int align, int eltsize)
{
elf_sects[elf_nsect].data = data;
elf_sects[elf_nsect].len = datalen;
elf_sects[elf_nsect].is_saa = is_saa;
elf_nsect++;
fwritelong((long)name, elffp);
fwritelong((long)type, elffp);
fwritelong((long)flags, elffp);
fwritelong(0L, elffp); /* no address, ever, in object files */
fwritelong(type == 0 ? 0L : elf_foffs, elffp);
fwritelong(datalen, elffp);
if (data)
elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
fwritelong((long)link, elffp);
fwritelong((long)info, elffp);
fwritelong((long)align, elffp);
fwritelong((long)eltsize, elffp);
}
static void elf_write_sections(void)
{
int i;
for (i = 0; i < elf_nsect; i++)
if (elf_sects[i].data) {
long len = elf_sects[i].len;
long reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
long align = reallen - len;
if (elf_sects[i].is_saa)
saa_fpwrite(elf_sects[i].data, elffp);
else
fwrite(elf_sects[i].data, len, 1, elffp);
fwrite(align_str, align, 1, elffp); /* 不足之数以 0 垫尾 */
}
}
static void elf_sect_write(struct Section *sect, const unsigned char *data, unsigned long len)
{
saa_wbytes(sect->data, data, len);
sect->len += len;
}
|