1 /* outelf.c output routines for the Netwide Assembler to produce
2 * ELF32 (i386 of course) object file format
4 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
5 * Julian Hall. All rights reserved. The software is
6 * redistributable under the licence given in the file "Licence"
7 * distributed in the NASM archive.
25 R_386_32
= 1, /* ordinary absolute relocation */
26 R_386_PC32
= 2, /* PC-relative relocation */
27 R_386_GOT32
= 3, /* an offset into GOT */
28 R_386_PLT32
= 4, /* a PC-relative offset into PLT */
29 R_386_COPY
= 5, /* ??? */
30 R_386_GLOB_DAT
= 6, /* ??? */
31 R_386_JUMP_SLOT
= 7, /* ??? */
32 R_386_RELATIVE
= 8, /* ??? */
33 R_386_GOTOFF
= 9, /* an offset from GOT base */
34 R_386_GOTPC
= 10, /* a PC-relative offset _to_ GOT */
35 /* These are GNU extensions, but useful */
36 R_386_16
= 20, /* A 16-bit absolute relocation */
37 R_386_PC16
= 21, /* A 16-bit PC-relative relocation */
38 R_386_8
= 22, /* An 8-bit absolute relocation */
39 R_386_PC8
= 23 /* An 8-bit PC-relative relocation */
44 long address
; /* relative to _start_ of section */
45 long symbol
; /* ELF symbol info thingy */
46 int type
; /* type of relocation */
50 long strpos
; /* string table position of name */
51 long section
; /* section ID of the symbol */
52 int type
; /* symbol type */
53 long value
; /* address, or COMMON variable align */
54 long size
; /* size of symbol */
55 long globnum
; /* symbol table offset if global */
56 struct Symbol
*next
; /* list of globals in each section */
57 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
58 char *name
; /* used temporarily if in above list */
61 #define SHT_PROGBITS 1
66 #define SHF_EXECINSTR 4
70 unsigned long len
, size
, nrelocs
;
72 int type
; /* SHT_PROGBITS or SHT_NOBITS */
73 int align
; /* alignment: power of two */
74 unsigned long flags
; /* section flags */
78 struct Reloc
*head
, **tail
;
79 struct Symbol
*gsyms
; /* global symbols in section */
83 static struct Section
**sects
;
84 static int nsects
, sectlen
;
86 #define SHSTR_DELTA 256
87 static char *shstrtab
;
88 static int shstrtablen
, shstrtabsize
;
90 static struct SAA
*syms
;
91 static unsigned long nlocals
, nglobs
;
95 static struct RAA
*bsym
;
97 static struct SAA
*strs
;
98 static unsigned long strslen
;
102 static evalfunc evaluate
;
104 static struct Symbol
*fwds
;
106 static char elf_module
[FILENAME_MAX
];
108 extern struct ofmt of_elf
;
110 #define SHN_ABS 0xFFF1
111 #define SHN_COMMON 0xFFF2
114 #define SYM_SECTION 0x04
115 #define SYM_GLOBAL 0x10
116 #define SYM_DATA 0x01
117 #define SYM_FUNCTION 0x02
119 #define GLOBAL_TEMP_BASE 15 /* bigger than any constant sym id */
121 #define SEG_ALIGN 16 /* alignment of sections in file */
122 #define SEG_ALIGN_1 (SEG_ALIGN-1)
124 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
126 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
127 static struct ELF_SECTDATA
{
132 static int elf_nsect
;
133 static long elf_foffs
;
135 static void elf_write(void);
136 static void elf_sect_write(struct Section
*, unsigned char *, unsigned long);
137 static void elf_section_header (int, int, int, void *, int, long,
139 static void elf_write_sections (void);
140 static struct SAA
*elf_build_symtab (long *, long *);
141 static struct SAA
*elf_build_reltab (long *, struct Reloc
*);
142 static void add_sectname (char *, char *);
145 * Special section numbers which are used to define ELF special
146 * symbols, which can be used with WRT to provide PIC relocation
149 static long elf_gotpc_sect
, elf_gotoff_sect
;
150 static long elf_got_sect
, elf_plt_sect
;
151 static long elf_sym_sect
;
153 static void elf_init(FILE *fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
158 (void) ldef
; /* placate optimisers */
160 nsects
= sectlen
= 0;
161 syms
= saa_init((long)sizeof(struct Symbol
));
162 nlocals
= nglobs
= 0;
165 saa_wbytes (strs
, "\0", 1L);
166 saa_wbytes (strs
, elf_module
, (long)(strlen(elf_module
)+1));
167 strslen
= 2+strlen(elf_module
);
169 shstrtablen
= shstrtabsize
= 0;;
170 add_sectname ("", "");
174 elf_gotpc_sect
= seg_alloc();
175 ldef("..gotpc", elf_gotpc_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
176 elf_gotoff_sect
= seg_alloc();
177 ldef("..gotoff", elf_gotoff_sect
+1, 0L, NULL
, FALSE
, FALSE
,&of_elf
,error
);
178 elf_got_sect
= seg_alloc();
179 ldef("..got", elf_got_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
180 elf_plt_sect
= seg_alloc();
181 ldef("..plt", elf_plt_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
182 elf_sym_sect
= seg_alloc();
183 ldef("..sym", elf_sym_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
185 def_seg
= seg_alloc();
188 static void elf_cleanup(int debuginfo
)
197 for (i
=0; i
<nsects
; i
++) {
198 if (sects
[i
]->type
!= SHT_NOBITS
)
199 saa_free (sects
[i
]->data
);
201 saa_free (sects
[i
]->rel
);
202 while (sects
[i
]->head
) {
204 sects
[i
]->head
= sects
[i
]->head
->next
;
214 static void add_sectname (char *firsthalf
, char *secondhalf
)
216 int len
= strlen(firsthalf
)+strlen(secondhalf
);
217 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
218 shstrtab
= nasm_realloc (shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
219 strcpy (shstrtab
+shstrtablen
, firsthalf
);
220 strcat (shstrtab
+shstrtablen
, secondhalf
);
221 shstrtablen
+= len
+1;
224 static int elf_make_section (char *name
, int type
, int flags
, int align
)
228 s
= nasm_malloc (sizeof(*s
));
230 if (type
!= SHT_NOBITS
)
231 s
->data
= saa_init (1L);
234 s
->len
= s
->size
= 0;
236 if (!strcmp(name
, ".text"))
239 s
->index
= seg_alloc();
240 add_sectname ("", name
);
241 s
->name
= nasm_malloc (1+strlen(name
));
242 strcpy (s
->name
, name
);
248 if (nsects
>= sectlen
)
249 sects
= nasm_realloc (sects
, (sectlen
+= SECT_DELTA
)*sizeof(*sects
));
255 static long elf_section_names (char *name
, int pass
, int *bits
)
258 int flags_and
, flags_or
, type
, align
, i
;
261 * Default is 32 bits.
269 while (*p
&& !isspace(*p
)) p
++;
271 flags_and
= flags_or
= type
= align
= 0;
273 while (*p
&& isspace(*p
)) p
++;
276 while (*p
&& !isspace(*p
)) p
++;
278 while (*p
&& isspace(*p
)) p
++;
280 if (!nasm_strnicmp(q
, "align=", 6)) {
284 if ( (align
-1) & align
) { /* means it's not a power of two */
285 error (ERR_NONFATAL
, "section alignment %d is not"
286 " a power of two", align
);
289 } else if (!nasm_stricmp(q
, "alloc")) {
290 flags_and
|= SHF_ALLOC
;
291 flags_or
|= SHF_ALLOC
;
292 } else if (!nasm_stricmp(q
, "noalloc")) {
293 flags_and
|= SHF_ALLOC
;
294 flags_or
&= ~SHF_ALLOC
;
295 } else if (!nasm_stricmp(q
, "exec")) {
296 flags_and
|= SHF_EXECINSTR
;
297 flags_or
|= SHF_EXECINSTR
;
298 } else if (!nasm_stricmp(q
, "noexec")) {
299 flags_and
|= SHF_EXECINSTR
;
300 flags_or
&= ~SHF_EXECINSTR
;
301 } else if (!nasm_stricmp(q
, "write")) {
302 flags_and
|= SHF_WRITE
;
303 flags_or
|= SHF_WRITE
;
304 } else if (!nasm_stricmp(q
, "nowrite")) {
305 flags_and
|= SHF_WRITE
;
306 flags_or
&= ~SHF_WRITE
;
307 } else if (!nasm_stricmp(q
, "progbits")) {
309 } else if (!nasm_stricmp(q
, "nobits")) {
314 if (!strcmp(name
, ".comment") ||
315 !strcmp(name
, ".shstrtab") ||
316 !strcmp(name
, ".symtab") ||
317 !strcmp(name
, ".strtab")) {
318 error (ERR_NONFATAL
, "attempt to redefine reserved section"
323 for (i
=0; i
<nsects
; i
++)
324 if (!strcmp(name
, sects
[i
]->name
))
327 if (!strcmp(name
, ".text"))
328 i
= elf_make_section (name
, SHT_PROGBITS
,
329 SHF_ALLOC
| SHF_EXECINSTR
, 16);
330 else if (!strcmp(name
, ".rodata"))
331 i
= elf_make_section (name
, SHT_PROGBITS
,
333 else if (!strcmp(name
, ".data"))
334 i
= elf_make_section (name
, SHT_PROGBITS
,
335 SHF_ALLOC
| SHF_WRITE
, 4);
336 else if (!strcmp(name
, ".bss"))
337 i
= elf_make_section (name
, SHT_NOBITS
,
338 SHF_ALLOC
| SHF_WRITE
, 4);
340 i
= elf_make_section (name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
342 sects
[i
]->type
= type
;
344 sects
[i
]->align
= align
;
345 sects
[i
]->flags
&= ~flags_and
;
346 sects
[i
]->flags
|= flags_or
;
347 } else if (pass
== 1) {
348 if (type
|| align
|| flags_and
)
349 error (ERR_WARNING
, "section attributes ignored on"
350 " redeclaration of section `%s'", name
);
353 return sects
[i
]->index
;
356 static void elf_deflabel (char *name
, long segment
, long offset
,
357 int is_global
, char *special
)
361 int special_used
= FALSE
;
363 #if defined(DEBUG) && DEBUG>2
364 fprintf(stderr
, " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
365 name
, segment
, offset
, is_global
, special
);
367 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
369 * This is a NASM special symbol. We never allow it into
370 * the ELF symbol table, even if it's a valid one. If it
371 * _isn't_ a valid one, we should barf immediately.
373 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
374 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
375 strcmp(name
, "..sym"))
376 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
380 if (is_global
== 3) {
383 * Fix up a forward-reference symbol size from the first
386 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
387 if (!strcmp((*s
)->name
, name
)) {
388 struct tokenval tokval
;
392 while (*p
&& !isspace(*p
)) p
++;
393 while (*p
&& isspace(*p
)) p
++;
396 tokval
.t_type
= TOKEN_INVALID
;
397 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
400 error (ERR_NONFATAL
, "cannot use relocatable"
401 " expression as symbol size");
403 (*s
)->size
= reloc_value(e
);
407 * Remove it from the list of unresolved sizes.
409 nasm_free ((*s
)->name
);
413 return; /* it wasn't an important one */
416 saa_wbytes (strs
, name
, (long)(1+strlen(name
)));
417 strslen
+= 1+strlen(name
);
419 sym
= saa_wstruct (syms
);
422 sym
->type
= is_global
? SYM_GLOBAL
: 0;
424 if (segment
== NO_SEG
)
425 sym
->section
= SHN_ABS
;
428 sym
->section
= SHN_UNDEF
;
429 if (nsects
== 0 && segment
== def_seg
) {
431 if (segment
!= elf_section_names (".text", 2, &tempint
))
432 error (ERR_PANIC
, "strange segment conditions in ELF driver");
433 sym
->section
= nsects
;
435 for (i
=0; i
<nsects
; i
++)
436 if (segment
== sects
[i
]->index
) {
443 if (is_global
== 2) {
446 sym
->section
= SHN_COMMON
;
448 * We have a common variable. Check the special text to see
449 * if it's a valid number and power of two; if so, store it
450 * as the alignment for the common variable.
454 sym
->value
= readnum (special
, &err
);
456 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
457 " valid number", special
);
458 else if ( (sym
->value
| (sym
->value
-1)) != 2*sym
->value
- 1)
459 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
460 " power of two", special
);
464 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
466 if (sym
->type
== SYM_GLOBAL
) {
468 * There's a problem here that needs fixing.
469 * If sym->section == SHN_ABS, then the first line of the
470 * else section causes a core dump, because its a reference
471 * beyond the end of the section array.
472 * This behaviour is exhibited by this code:
476 * I'm not sure how to procede, because I haven't got the
477 * first clue about how ELF works, so I don't know what to
478 * do with it. Furthermore, I'm not sure what the rest of this
479 * section of code does. Help?
481 * For now, I'll see if doing absolutely nothing with it will
484 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
)
486 bsym
= raa_write (bsym
, segment
, nglobs
);
488 else if (sym
->section
!= SHN_ABS
)
491 * This is a global symbol; so we must add it to the linked
492 * list of global symbols in its section. We'll push it on
493 * the beginning of the list, because it doesn't matter
494 * much which end we put it on and it's easier like this.
496 * In addition, we check the special text for symbol
497 * type and size information.
499 sym
->next
= sects
[sym
->section
-1]->gsyms
;
500 sects
[sym
->section
-1]->gsyms
= sym
;
503 int n
= strcspn(special
, " ");
505 if (!nasm_strnicmp(special
, "function", n
))
506 sym
->type
|= SYM_FUNCTION
;
507 else if (!nasm_strnicmp(special
, "data", n
) ||
508 !nasm_strnicmp(special
, "object", n
))
509 sym
->type
|= SYM_DATA
;
511 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
514 struct tokenval tokval
;
517 char *saveme
=stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
519 while (special
[n
] && isspace(special
[n
]))
522 * We have a size expression; attempt to
526 stdscan_bufptr
= special
+n
;
527 tokval
.t_type
= TOKEN_INVALID
;
528 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
, NULL
);
532 sym
->name
= nasm_strdup(name
);
535 error (ERR_NONFATAL
, "cannot use relocatable"
536 " expression as symbol size");
538 sym
->size
= reloc_value(e
);
540 stdscan_bufptr
=saveme
; /* bugfix? fbk 8/10/00 */
545 sym
->globnum
= nglobs
;
550 if (special
&& !special_used
)
551 error(ERR_NONFATAL
, "no special symbol features supported here");
554 static void elf_add_reloc (struct Section
*sect
, long segment
,
559 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
560 sect
->tail
= &r
->next
;
563 r
->address
= sect
->len
;
564 if (segment
== NO_SEG
)
569 for (i
=0; i
<nsects
; i
++)
570 if (segment
== sects
[i
]->index
)
573 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
581 * This routine deals with ..got and ..sym relocations: the more
582 * complicated kinds. In shared-library writing, some relocations
583 * with respect to global symbols must refer to the precise symbol
584 * rather than referring to an offset from the base of the section
585 * _containing_ the symbol. Such relocations call to this routine,
586 * which searches the symbol list for the symbol in question.
588 * R_386_GOT32 references require the _exact_ symbol address to be
589 * used; R_386_32 references can be at an offset from the symbol.
590 * The boolean argument `exact' tells us this.
592 * Return value is the adjusted value of `addr', having become an
593 * offset from the symbol rather than the section. Should always be
594 * zero when returning from an exact call.
596 * Limitation: if you define two symbols at the same place,
597 * confusion will occur.
599 * Inefficiency: we search, currently, using a linked list which
600 * isn't even necessarily sorted.
602 static long elf_add_gsym_reloc (struct Section
*sect
,
603 long segment
, long offset
,
608 struct Symbol
*sym
, *sm
;
612 * First look up the segment/offset pair and find a global
613 * symbol corresponding to it. If it's not one of our segments,
614 * then it must be an external symbol, in which case we're fine
615 * doing a normal elf_add_reloc after first sanity-checking
616 * that the offset from the symbol is zero.
619 for (i
=0; i
<nsects
; i
++)
620 if (segment
== sects
[i
]->index
) {
625 if (exact
&& offset
!= 0)
626 error (ERR_NONFATAL
, "unable to find a suitable global symbol"
627 " for this reference");
629 elf_add_reloc (sect
, segment
, type
);
635 * Find a symbol pointing _exactly_ at this one.
637 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
638 if (sym
->value
== offset
)
642 * Find the nearest symbol below this one.
645 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
646 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
650 error (ERR_NONFATAL
, "unable to find a suitable global symbol"
651 " for this reference");
655 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
656 sect
->tail
= &r
->next
;
659 r
->address
= sect
->len
;
660 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
665 return offset
- sym
->value
;
668 static void elf_out (long segto
, void *data
, unsigned long type
,
669 long segment
, long wrt
)
672 long realbytes
= type
& OUT_SIZMASK
;
674 unsigned char mydata
[4], *p
;
680 * handle absolute-assembly (structure definitions)
682 if (segto
== NO_SEG
) {
683 if (type
!= OUT_RESERVE
)
684 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
690 for (i
=0; i
<nsects
; i
++)
691 if (segto
== sects
[i
]->index
) {
696 int tempint
; /* ignored */
697 if (segto
!= elf_section_names (".text", 2, &tempint
))
698 error (ERR_PANIC
, "strange segment conditions in ELF driver");
703 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
704 error(ERR_WARNING
, "attempt to initialise memory in"
705 " BSS section `%s': ignored", s
->name
);
706 if (type
== OUT_REL2ADR
)
708 else if (type
== OUT_REL4ADR
)
714 if (type
== OUT_RESERVE
) {
715 if (s
->type
== SHT_PROGBITS
) {
716 error(ERR_WARNING
, "uninitialised space declared in"
717 " non-BSS section `%s': zeroing", s
->name
);
718 elf_sect_write (s
, NULL
, realbytes
);
721 } else if (type
== OUT_RAWDATA
) {
722 if (segment
!= NO_SEG
)
723 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
724 elf_sect_write (s
, data
, realbytes
);
725 } else if (type
== OUT_ADDRESS
) {
727 addr
= *(long *)data
;
728 if (segment
!= NO_SEG
) {
730 error(ERR_NONFATAL
, "ELF format does not support"
731 " segment base references");
734 if ( realbytes
== 2 ) {
736 elf_add_reloc (s
, segment
, R_386_16
);
738 elf_add_reloc (s
, segment
, R_386_32
);
740 } else if (wrt
== elf_gotpc_sect
+1) {
742 * The user will supply GOT relative to $$. ELF
743 * will let us have GOT relative to $. So we
744 * need to fix up the data item by $-$$.
747 elf_add_reloc (s
, segment
, R_386_GOTPC
);
748 } else if (wrt
== elf_gotoff_sect
+1) {
749 elf_add_reloc (s
, segment
, R_386_GOTOFF
);
750 } else if (wrt
== elf_got_sect
+1) {
751 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
753 } else if (wrt
== elf_sym_sect
+1) {
754 if ( realbytes
== 2 ) {
756 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
759 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
762 } else if (wrt
== elf_plt_sect
+1) {
763 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
764 "relative PLT references");
766 error (ERR_NONFATAL
, "ELF format does not support this"
768 wrt
= NO_SEG
; /* we can at least _try_ to continue */
774 error(ERR_WARNING
|ERR_WARN_GNUELF
,
775 "16-bit relocations in ELF is a GNU extension");
776 WRITESHORT (p
, addr
);
778 if (realbytes
!= 4 && segment
!= NO_SEG
) {
779 error (ERR_NONFATAL
, "Unsupported non-32-bit ELF relocation");
783 elf_sect_write (s
, mydata
, realbytes
);
784 } else if (type
== OUT_REL2ADR
) {
785 if (segment
== segto
)
786 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
787 if (segment
!= NO_SEG
&& segment
% 2) {
788 error(ERR_NONFATAL
, "ELF format does not support"
789 " segment base references");
792 error (ERR_WARNING
|ERR_WARN_GNUELF
,
793 "16-bit relocations in ELF is a GNU extension");
794 elf_add_reloc (s
, segment
, R_386_PC16
);
796 error (ERR_NONFATAL
, "Unsupported non-32-bit ELF relocation");
800 WRITESHORT (p
, *(long*)data
- realbytes
);
801 elf_sect_write (s
, mydata
, 2L);
802 } else if (type
== OUT_REL4ADR
) {
803 if (segment
== segto
)
804 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
805 if (segment
!= NO_SEG
&& segment
% 2) {
806 error(ERR_NONFATAL
, "ELF format does not support"
807 " segment base references");
810 elf_add_reloc (s
, segment
, R_386_PC32
);
811 } else if (wrt
== elf_plt_sect
+1) {
812 elf_add_reloc (s
, segment
, R_386_PLT32
);
813 } else if (wrt
== elf_gotpc_sect
+1 ||
814 wrt
== elf_gotoff_sect
+1 ||
815 wrt
== elf_got_sect
+1) {
816 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
817 "relative GOT references");
819 error (ERR_NONFATAL
, "ELF format does not support this"
821 wrt
= NO_SEG
; /* we can at least _try_ to continue */
825 WRITELONG (p
, *(long*)data
- realbytes
);
826 elf_sect_write (s
, mydata
, 4L);
830 static void elf_write(void)
832 int nsections
, align
;
839 long symtablen
, symtablocal
;
842 * Work out how many sections we will have. We have SHN_UNDEF,
843 * then the flexible user sections, then the four fixed
844 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
845 * then optionally relocation sections for the user sections.
847 nsections
= 5; /* SHN_UNDEF and the fixed ones */
848 add_sectname ("", ".comment");
849 add_sectname ("", ".shstrtab");
850 add_sectname ("", ".symtab");
851 add_sectname ("", ".strtab");
852 for (i
=0; i
<nsects
; i
++) {
853 nsections
++; /* for the section itself */
854 if (sects
[i
]->head
) {
855 nsections
++; /* for its relocations */
856 add_sectname (".rel", sects
[i
]->name
);
864 commlen
= 2+sprintf(comment
+1, "The Netwide Assembler %s", NASM_VER
);
867 * Output the ELF header.
869 fwrite ("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
870 fwriteshort (1, elffp
); /* ET_REL relocatable file */
871 fwriteshort (3, elffp
); /* EM_386 processor ID */
872 fwritelong (1L, elffp
); /* EV_CURRENT file format version */
873 fwritelong (0L, elffp
); /* no entry point */
874 fwritelong (0L, elffp
); /* no program header table */
875 fwritelong (0x40L
, elffp
); /* section headers straight after
876 * ELF header plus alignment */
877 fwritelong (0L, elffp
); /* 386 defines no special flags */
878 fwriteshort (0x34, elffp
); /* size of ELF header */
879 fwriteshort (0, elffp
); /* no program header table, again */
880 fwriteshort (0, elffp
); /* still no program header table */
881 fwriteshort (0x28, elffp
); /* size of section header */
882 fwriteshort (nsections
, elffp
); /* number of sections */
883 fwriteshort (nsects
+2, elffp
); /* string table section index for
884 * section header table */
885 fwritelong (0L, elffp
); /* align to 0x40 bytes */
886 fwritelong (0L, elffp
);
887 fwritelong (0L, elffp
);
890 * Build the symbol table and relocation tables.
892 symtab
= elf_build_symtab (&symtablen
, &symtablocal
);
893 for (i
=0; i
<nsects
; i
++)
895 sects
[i
]->rel
= elf_build_reltab (§s
[i
]->rellen
,
899 * Now output the section header table.
902 elf_foffs
= 0x40 + 0x28 * nsections
;
903 align
= ((elf_foffs
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
906 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
908 elf_section_header (0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
910 for (i
=0; i
<nsects
; i
++) {
911 elf_section_header (p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
912 (sects
[i
]->type
== SHT_PROGBITS
?
913 sects
[i
]->data
: NULL
), TRUE
,
914 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
917 elf_section_header (p
- shstrtab
, 1, 0, comment
, FALSE
,
918 (long)commlen
, 0, 0, 1, 0);/* .comment */
920 elf_section_header (p
- shstrtab
, 3, 0, shstrtab
, FALSE
,
921 (long)shstrtablen
, 0, 0, 1, 0);/* .shstrtab */
923 elf_section_header (p
- shstrtab
, 2, 0, symtab
, TRUE
,
924 symtablen
, nsects
+4, symtablocal
, 4, 16);/* .symtab */
926 elf_section_header (p
- shstrtab
, 3, 0, strs
, TRUE
,
927 strslen
, 0, 0, 1, 0); /* .strtab */
928 for (i
=0; i
<nsects
; i
++) if (sects
[i
]->head
) {
930 elf_section_header (p
- shstrtab
, 9, 0, sects
[i
]->rel
, TRUE
,
931 sects
[i
]->rellen
, nsects
+3, i
+1, 4, 8);
934 fwrite (align_str
, align
, 1, elffp
);
937 * Now output the sections.
939 elf_write_sections();
941 nasm_free (elf_sects
);
945 static struct SAA
*elf_build_symtab (long *len
, long *local
)
947 struct SAA
*s
= saa_init(1L);
949 unsigned char entry
[16], *p
;
955 * First, an all-zeros entry, required by the ELF spec.
957 saa_wbytes (s
, NULL
, 16L); /* null symbol table entry */
962 * Next, an entry for the file name.
965 WRITELONG (p
, 1); /* we know it's 1st thing in strtab */
966 WRITELONG (p
, 0); /* no value */
967 WRITELONG (p
, 0); /* no size either */
968 WRITESHORT (p
, 4); /* type FILE */
969 WRITESHORT (p
, SHN_ABS
);
970 saa_wbytes (s
, entry
, 16L);
975 * Now some standard symbols defining the segments, for relocation
978 for (i
= 1; i
<= nsects
+1; i
++) {
980 WRITELONG (p
, 0); /* no symbol name */
981 WRITELONG (p
, 0); /* offset zero */
982 WRITELONG (p
, 0); /* size zero */
983 WRITESHORT (p
, 3); /* local section-type thing */
984 WRITESHORT (p
, (i
==1 ? SHN_ABS
: i
-1)); /* the section id */
985 saa_wbytes (s
, entry
, 16L);
991 * Now the other local symbols.
994 while ( (sym
= saa_rstruct (syms
)) ) {
995 if (sym
->type
& SYM_GLOBAL
)
998 WRITELONG (p
, sym
->strpos
);
999 WRITELONG (p
, sym
->value
);
1000 WRITELONG (p
, sym
->size
);
1001 WRITESHORT (p
, sym
->type
); /* local non-typed thing */
1002 WRITESHORT (p
, sym
->section
);
1003 saa_wbytes (s
, entry
, 16L);
1009 * Now the global symbols.
1012 while ( (sym
= saa_rstruct (syms
)) ) {
1013 if (!(sym
->type
& SYM_GLOBAL
))
1016 WRITELONG (p
, sym
->strpos
);
1017 WRITELONG (p
, sym
->value
);
1018 WRITELONG (p
, sym
->size
);
1019 WRITESHORT (p
, sym
->type
); /* global non-typed thing */
1020 WRITESHORT (p
, sym
->section
);
1021 saa_wbytes (s
, entry
, 16L);
1028 static struct SAA
*elf_build_reltab (long *len
, struct Reloc
*r
) {
1030 unsigned char *p
, entry
[8];
1039 long sym
= r
->symbol
;
1041 if (sym
>= GLOBAL_TEMP_BASE
)
1042 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+3) + nlocals
;
1045 WRITELONG (p
, r
->address
);
1046 WRITELONG (p
, (sym
<< 8) + r
->type
);
1047 saa_wbytes (s
, entry
, 8L);
1056 static void elf_section_header (int name
, int type
, int flags
,
1057 void *data
, int is_saa
, long datalen
,
1058 int link
, int info
, int align
, int eltsize
)
1060 elf_sects
[elf_nsect
].data
= data
;
1061 elf_sects
[elf_nsect
].len
= datalen
;
1062 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1065 fwritelong ((long)name
, elffp
);
1066 fwritelong ((long)type
, elffp
);
1067 fwritelong ((long)flags
, elffp
);
1068 fwritelong (0L, elffp
); /* no address, ever, in object files */
1069 fwritelong (type
== 0 ? 0L : elf_foffs
, elffp
);
1070 fwritelong (datalen
, elffp
);
1072 elf_foffs
+= (datalen
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1073 fwritelong ((long)link
, elffp
);
1074 fwritelong ((long)info
, elffp
);
1075 fwritelong ((long)align
, elffp
);
1076 fwritelong ((long)eltsize
, elffp
);
1079 static void elf_write_sections (void)
1082 for (i
= 0; i
< elf_nsect
; i
++)
1083 if (elf_sects
[i
].data
) {
1084 long len
= elf_sects
[i
].len
;
1085 long reallen
= (len
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1086 long align
= reallen
- len
;
1087 if (elf_sects
[i
].is_saa
)
1088 saa_fpwrite (elf_sects
[i
].data
, elffp
);
1090 fwrite (elf_sects
[i
].data
, len
, 1, elffp
);
1091 fwrite (align_str
, align
, 1, elffp
);
1095 static void elf_sect_write (struct Section
*sect
,
1096 unsigned char *data
, unsigned long len
)
1098 saa_wbytes (sect
->data
, data
, len
);
1102 static long elf_segbase (long segment
)
1107 static int elf_directive (char *directive
, char *value
, int pass
)
1112 static void elf_filename (char *inname
, char *outname
, efunc error
)
1114 strcpy(elf_module
, inname
);
1115 standard_extension (inname
, outname
, ".o", error
);
1118 static const char *elf_stdmac
[] = {
1119 "%define __SECT__ [section .text]",
1120 "%macro __NASM_CDecl__ 1",
1125 static int elf_set_info(enum geninfo type
, char **val
)
1130 struct ofmt of_elf
= {
1131 "ELF32 (i386) object files (e.g. Linux)",