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.
24 #define R_386_32 1 /* ordinary absolute relocation */
25 #define R_386_PC32 2 /* PC-relative relocation */
26 #define R_386_GOT32 3 /* an offset into GOT */
27 #define R_386_PLT32 4 /* a PC-relative offset into PLT */
28 #define R_386_GOTOFF 9 /* an offset from GOT base */
29 #define R_386_GOTPC 10 /* a PC-relative offset _to_ GOT */
33 long address
; /* relative to _start_ of section */
34 long symbol
; /* ELF symbol info thingy */
35 int type
; /* type of relocation */
39 long strpos
; /* string table position of name */
40 long section
; /* section ID of the symbol */
41 int type
; /* symbol type */
42 long value
; /* address, or COMMON variable align */
43 long size
; /* size of symbol */
44 long globnum
; /* symbol table offset if global */
45 struct Symbol
*next
; /* list of globals in each section */
46 struct Symbol
*nextfwd
; /* list of unresolved-size symbols */
47 char *name
; /* used temporarily if in above list */
50 #define SHT_PROGBITS 1
55 #define SHF_EXECINSTR 4
59 unsigned long len
, size
, nrelocs
;
61 int type
; /* SHT_PROGBITS or SHT_NOBITS */
62 int align
; /* alignment: power of two */
63 unsigned long flags
; /* section flags */
67 struct Reloc
*head
, **tail
;
68 struct Symbol
*gsyms
; /* global symbols in section */
72 static struct Section
**sects
;
73 static int nsects
, sectlen
;
75 #define SHSTR_DELTA 256
76 static char *shstrtab
;
77 static int shstrtablen
, shstrtabsize
;
79 static struct SAA
*syms
;
80 static unsigned long nlocals
, nglobs
;
84 static struct RAA
*bsym
;
86 static struct SAA
*strs
;
87 static unsigned long strslen
;
91 static evalfunc evaluate
;
93 static struct Symbol
*fwds
;
95 static char elf_module
[FILENAME_MAX
];
97 extern struct ofmt of_elf
;
99 #define SHN_ABS 0xFFF1
100 #define SHN_COMMON 0xFFF2
103 #define SYM_SECTION 0x04
104 #define SYM_GLOBAL 0x10
105 #define SYM_DATA 0x01
106 #define SYM_FUNCTION 0x02
108 #define GLOBAL_TEMP_BASE 6 /* bigger than any constant sym id */
110 #define SEG_ALIGN 16 /* alignment of sections in file */
111 #define SEG_ALIGN_1 (SEG_ALIGN-1)
113 static const char align_str
[SEG_ALIGN
] = ""; /* ANSI will pad this with 0s */
115 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
116 static struct ELF_SECTDATA
{
121 static int elf_nsect
;
122 static long elf_foffs
;
124 static void elf_write(void);
125 static void elf_sect_write(struct Section
*, unsigned char *, unsigned long);
126 static void elf_section_header (int, int, int, void *, int, long,
128 static void elf_write_sections (void);
129 static struct SAA
*elf_build_symtab (long *, long *);
130 static struct SAA
*elf_build_reltab (long *, struct Reloc
*);
131 static void add_sectname (char *, char *);
134 * Special section numbers which are used to define ELF special
135 * symbols, which can be used with WRT to provide PIC relocation
138 static long elf_gotpc_sect
, elf_gotoff_sect
;
139 static long elf_got_sect
, elf_plt_sect
;
140 static long elf_sym_sect
;
142 static void elf_init(FILE *fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
147 (void) ldef
; /* placate optimisers */
149 nsects
= sectlen
= 0;
150 syms
= saa_init((long)sizeof(struct Symbol
));
151 nlocals
= nglobs
= 0;
154 saa_wbytes (strs
, "\0", 1L);
155 saa_wbytes (strs
, elf_module
, (long)(strlen(elf_module
)+1));
156 strslen
= 2+strlen(elf_module
);
158 shstrtablen
= shstrtabsize
= 0;;
159 add_sectname ("", "");
163 elf_gotpc_sect
= seg_alloc();
164 ldef("..gotpc", elf_gotpc_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
165 elf_gotoff_sect
= seg_alloc();
166 ldef("..gotoff", elf_gotoff_sect
+1, 0L, NULL
, FALSE
, FALSE
,&of_elf
,error
);
167 elf_got_sect
= seg_alloc();
168 ldef("..got", elf_got_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
169 elf_plt_sect
= seg_alloc();
170 ldef("..plt", elf_plt_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
171 elf_sym_sect
= seg_alloc();
172 ldef("..sym", elf_sym_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
174 def_seg
= seg_alloc();
177 static void elf_cleanup(int debuginfo
)
186 for (i
=0; i
<nsects
; i
++) {
187 if (sects
[i
]->type
!= SHT_NOBITS
)
188 saa_free (sects
[i
]->data
);
190 saa_free (sects
[i
]->rel
);
191 while (sects
[i
]->head
) {
193 sects
[i
]->head
= sects
[i
]->head
->next
;
203 static void add_sectname (char *firsthalf
, char *secondhalf
)
205 int len
= strlen(firsthalf
)+strlen(secondhalf
);
206 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
207 shstrtab
= nasm_realloc (shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
208 strcpy (shstrtab
+shstrtablen
, firsthalf
);
209 strcat (shstrtab
+shstrtablen
, secondhalf
);
210 shstrtablen
+= len
+1;
213 static int elf_make_section (char *name
, int type
, int flags
, int align
)
217 s
= nasm_malloc (sizeof(*s
));
219 if (type
!= SHT_NOBITS
)
220 s
->data
= saa_init (1L);
223 s
->len
= s
->size
= 0;
225 if (!strcmp(name
, ".text"))
228 s
->index
= seg_alloc();
229 add_sectname ("", name
);
230 s
->name
= nasm_malloc (1+strlen(name
));
231 strcpy (s
->name
, name
);
237 if (nsects
>= sectlen
)
238 sects
= nasm_realloc (sects
, (sectlen
+= SECT_DELTA
)*sizeof(*sects
));
244 static long elf_section_names (char *name
, int pass
, int *bits
)
247 int flags_and
, flags_or
, type
, align
, i
;
250 * Default is 32 bits.
258 while (*p
&& !isspace(*p
)) p
++;
260 flags_and
= flags_or
= type
= align
= 0;
262 while (*p
&& isspace(*p
)) p
++;
265 while (*p
&& !isspace(*p
)) p
++;
267 while (*p
&& isspace(*p
)) p
++;
269 if (!nasm_strnicmp(q
, "align=", 6)) {
273 if ( (align
-1) & align
) { /* means it's not a power of two */
274 error (ERR_NONFATAL
, "section alignment %d is not"
275 " a power of two", align
);
278 } else if (!nasm_stricmp(q
, "alloc")) {
279 flags_and
|= SHF_ALLOC
;
280 flags_or
|= SHF_ALLOC
;
281 } else if (!nasm_stricmp(q
, "noalloc")) {
282 flags_and
|= SHF_ALLOC
;
283 flags_or
&= ~SHF_ALLOC
;
284 } else if (!nasm_stricmp(q
, "exec")) {
285 flags_and
|= SHF_EXECINSTR
;
286 flags_or
|= SHF_EXECINSTR
;
287 } else if (!nasm_stricmp(q
, "noexec")) {
288 flags_and
|= SHF_EXECINSTR
;
289 flags_or
&= ~SHF_EXECINSTR
;
290 } else if (!nasm_stricmp(q
, "write")) {
291 flags_and
|= SHF_WRITE
;
292 flags_or
|= SHF_WRITE
;
293 } else if (!nasm_stricmp(q
, "nowrite")) {
294 flags_and
|= SHF_WRITE
;
295 flags_or
&= ~SHF_WRITE
;
296 } else if (!nasm_stricmp(q
, "progbits")) {
298 } else if (!nasm_stricmp(q
, "nobits")) {
303 if (!strcmp(name
, ".comment") ||
304 !strcmp(name
, ".shstrtab") ||
305 !strcmp(name
, ".symtab") ||
306 !strcmp(name
, ".strtab")) {
307 error (ERR_NONFATAL
, "attempt to redefine reserved section"
312 for (i
=0; i
<nsects
; i
++)
313 if (!strcmp(name
, sects
[i
]->name
))
316 if (!strcmp(name
, ".text"))
317 i
= elf_make_section (name
, SHT_PROGBITS
,
318 SHF_ALLOC
| SHF_EXECINSTR
, 16);
319 else if (!strcmp(name
, ".data"))
320 i
= elf_make_section (name
, SHT_PROGBITS
,
321 SHF_ALLOC
| SHF_WRITE
, 4);
322 else if (!strcmp(name
, ".bss"))
323 i
= elf_make_section (name
, SHT_NOBITS
,
324 SHF_ALLOC
| SHF_WRITE
, 4);
326 i
= elf_make_section (name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
328 sects
[i
]->type
= type
;
330 sects
[i
]->align
= align
;
331 sects
[i
]->flags
&= ~flags_and
;
332 sects
[i
]->flags
|= flags_or
;
333 } else if (pass
== 1) {
334 if (type
|| align
|| flags_and
)
335 error (ERR_WARNING
, "section attributes ignored on"
336 " redeclaration of section `%s'", name
);
339 return sects
[i
]->index
;
342 static void elf_deflabel (char *name
, long segment
, long offset
,
343 int is_global
, char *special
)
347 int special_used
= FALSE
;
349 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
351 * This is a NASM special symbol. We never allow it into
352 * the ELF symbol table, even if it's a valid one. If it
353 * _isn't_ a valid one, we should barf immediately.
355 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
356 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
357 strcmp(name
, "..sym"))
358 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
362 if (is_global
== 3) {
365 * Fix up a forward-reference symbol size from the first
368 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
369 if (!strcmp((*s
)->name
, name
)) {
370 struct tokenval tokval
;
374 while (*p
&& !isspace(*p
)) p
++;
375 while (*p
&& isspace(*p
)) p
++;
378 tokval
.t_type
= TOKEN_INVALID
;
379 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
382 error (ERR_NONFATAL
, "cannot use relocatable"
383 " expression as symbol size");
385 (*s
)->size
= reloc_value(e
);
389 * Remove it from the list of unresolved sizes.
391 nasm_free ((*s
)->name
);
395 return; /* it wasn't an important one */
398 saa_wbytes (strs
, name
, (long)(1+strlen(name
)));
399 strslen
+= 1+strlen(name
);
401 sym
= saa_wstruct (syms
);
404 sym
->type
= is_global
? SYM_GLOBAL
: 0;
406 if (segment
== NO_SEG
)
407 sym
->section
= SHN_ABS
;
410 sym
->section
= SHN_UNDEF
;
411 if (nsects
== 0 && segment
== def_seg
) {
413 if (segment
!= elf_section_names (".text", 2, &tempint
))
414 error (ERR_PANIC
, "strange segment conditions in ELF driver");
415 sym
->section
= nsects
;
417 for (i
=0; i
<nsects
; i
++)
418 if (segment
== sects
[i
]->index
) {
425 if (is_global
== 2) {
428 sym
->section
= SHN_COMMON
;
430 * We have a common variable. Check the special text to see
431 * if it's a valid number and power of two; if so, store it
432 * as the alignment for the common variable.
436 sym
->value
= readnum (special
, &err
);
438 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
439 " valid number", special
);
440 else if ( (sym
->value
| (sym
->value
-1)) != 2*sym
->value
- 1)
441 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
442 " power of two", special
);
446 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
448 if (sym
->type
== SYM_GLOBAL
) {
450 * If sym->section == SHN_ABS, then the first line of the else
451 * section would cause a core dump, because it's a reference
452 * beyond the end of the section array.
453 * This case happens with code such as this:
457 * We avoid the crash by skipping over the else section. It's
458 * a bit unclear whether or not this is the right thing to do,
459 * but it appears to work correctly.
461 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
)
463 bsym
= raa_write (bsym
, segment
, nglobs
);
465 else if (sym
->section
!= SHN_ABS
)
468 * This is a global symbol; so we must add it to the linked
469 * list of global symbols in its section. We'll push it on
470 * the beginning of the list, because it doesn't matter
471 * much which end we put it on and it's easier like this.
473 * In addition, we check the special text for symbol
474 * type and size information.
476 sym
->next
= sects
[sym
->section
-1]->gsyms
;
477 sects
[sym
->section
-1]->gsyms
= sym
;
480 int n
= strcspn(special
, " ");
482 if (!nasm_strnicmp(special
, "function", n
))
483 sym
->type
|= SYM_FUNCTION
;
484 else if (!nasm_strnicmp(special
, "data", n
) ||
485 !nasm_strnicmp(special
, "object", n
))
486 sym
->type
|= SYM_DATA
;
488 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
491 struct tokenval tokval
;
494 char *saveme
=stdscan_bufptr
; /*bf*/
496 while (special
[n
] && isspace(special
[n
]))
499 * We have a size expression; attempt to
503 stdscan_bufptr
= special
+n
;
504 tokval
.t_type
= TOKEN_INVALID
;
505 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
, NULL
);
509 sym
->name
= nasm_strdup(name
);
512 error (ERR_NONFATAL
, "cannot use relocatable"
513 " expression as symbol size");
515 sym
->size
= reloc_value(e
);
517 stdscan_bufptr
=saveme
; /*bf*/
522 sym
->globnum
= nglobs
;
527 if (special
&& !special_used
)
528 error(ERR_NONFATAL
, "no special symbol features supported here");
531 static void elf_add_reloc (struct Section
*sect
, long segment
,
536 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
537 sect
->tail
= &r
->next
;
540 r
->address
= sect
->len
;
541 if (segment
== NO_SEG
)
546 for (i
=0; i
<nsects
; i
++)
547 if (segment
== sects
[i
]->index
)
550 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
558 * This routine deals with ..got and ..sym relocations: the more
559 * complicated kinds. In shared-library writing, some relocations
560 * with respect to global symbols must refer to the precise symbol
561 * rather than referring to an offset from the base of the section
562 * _containing_ the symbol. Such relocations call to this routine,
563 * which searches the symbol list for the symbol in question.
565 * R_386_GOT32 references require the _exact_ symbol address to be
566 * used; R_386_32 references can be at an offset from the symbol.
567 * The boolean argument `exact' tells us this.
569 * Return value is the adjusted value of `addr', having become an
570 * offset from the symbol rather than the section. Should always be
571 * zero when returning from an exact call.
573 * Limitation: if you define two symbols at the same place,
574 * confusion will occur.
576 * Inefficiency: we search, currently, using a linked list which
577 * isn't even necessarily sorted.
579 static long elf_add_gsym_reloc (struct Section
*sect
,
580 long segment
, long offset
,
585 struct Symbol
*sym
, *sm
;
589 * First look up the segment/offset pair and find a global
590 * symbol corresponding to it. If it's not one of our segments,
591 * then it must be an external symbol, in which case we're fine
592 * doing a normal elf_add_reloc after first sanity-checking
593 * that the offset from the symbol is zero.
596 for (i
=0; i
<nsects
; i
++)
597 if (segment
== sects
[i
]->index
) {
602 if (exact
&& offset
!= 0)
603 error (ERR_NONFATAL
, "unable to find a suitable global symbol"
604 " for this reference");
606 elf_add_reloc (sect
, segment
, type
);
612 * Find a symbol pointing _exactly_ at this one.
614 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
615 if (sym
->value
== offset
)
619 * Find the nearest symbol below this one.
622 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
623 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
627 error (ERR_NONFATAL
, "unable to find a suitable global symbol"
628 " for this reference");
632 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
633 sect
->tail
= &r
->next
;
636 r
->address
= sect
->len
;
637 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
642 return offset
- sym
->value
;
645 static void elf_out (long segto
, void *data
, unsigned long type
,
646 long segment
, long wrt
)
649 long realbytes
= type
& OUT_SIZMASK
;
651 unsigned char mydata
[4], *p
;
657 * handle absolute-assembly (structure definitions)
659 if (segto
== NO_SEG
) {
660 if (type
!= OUT_RESERVE
)
661 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
667 for (i
=0; i
<nsects
; i
++)
668 if (segto
== sects
[i
]->index
) {
673 int tempint
; /* ignored */
674 if (segto
!= elf_section_names (".text", 2, &tempint
))
675 error (ERR_PANIC
, "strange segment conditions in ELF driver");
680 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
681 error(ERR_WARNING
, "attempt to initialise memory in"
682 " BSS section `%s': ignored", s
->name
);
683 if (type
== OUT_REL2ADR
)
685 else if (type
== OUT_REL4ADR
)
691 if (type
== OUT_RESERVE
) {
692 if (s
->type
== SHT_PROGBITS
) {
693 error(ERR_WARNING
, "uninitialised space declared in"
694 " non-BSS section `%s': zeroing", s
->name
);
695 elf_sect_write (s
, NULL
, realbytes
);
698 } else if (type
== OUT_RAWDATA
) {
699 if (segment
!= NO_SEG
)
700 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
701 elf_sect_write (s
, data
, realbytes
);
702 } else if (type
== OUT_ADDRESS
) {
703 addr
= *(long *)data
;
704 if (segment
!= NO_SEG
) {
706 error(ERR_NONFATAL
, "ELF format does not support"
707 " segment base references");
710 elf_add_reloc (s
, segment
, R_386_32
);
711 } else if (wrt
== elf_gotpc_sect
+1) {
713 * The user will supply GOT relative to $$. ELF
714 * will let us have GOT relative to $. So we
715 * need to fix up the data item by $-$$.
718 elf_add_reloc (s
, segment
, R_386_GOTPC
);
719 } else if (wrt
== elf_gotoff_sect
+1) {
720 elf_add_reloc (s
, segment
, R_386_GOTOFF
);
721 } else if (wrt
== elf_got_sect
+1) {
722 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
724 } else if (wrt
== elf_sym_sect
+1) {
725 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
727 } else if (wrt
== elf_plt_sect
+1) {
728 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
729 "relative PLT references");
731 error (ERR_NONFATAL
, "ELF format does not support this"
733 wrt
= NO_SEG
; /* we can at least _try_ to continue */
738 if (realbytes
!= 4 && segment
!= NO_SEG
)
739 error (ERR_NONFATAL
, "ELF format does not support non-32-bit"
742 elf_sect_write (s
, mydata
, realbytes
);
743 } else if (type
== OUT_REL2ADR
) {
744 error (ERR_NONFATAL
, "ELF format does not support 16-bit"
746 } else if (type
== OUT_REL4ADR
) {
747 if (segment
== segto
)
748 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
749 if (segment
!= NO_SEG
&& segment
% 2) {
750 error(ERR_NONFATAL
, "ELF format does not support"
751 " segment base references");
754 elf_add_reloc (s
, segment
, R_386_PC32
);
755 } else if (wrt
== elf_plt_sect
+1) {
756 elf_add_reloc (s
, segment
, R_386_PLT32
);
757 } else if (wrt
== elf_gotpc_sect
+1 ||
758 wrt
== elf_gotoff_sect
+1 ||
759 wrt
== elf_got_sect
+1) {
760 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
761 "relative GOT references");
763 error (ERR_NONFATAL
, "ELF format does not support this"
765 wrt
= NO_SEG
; /* we can at least _try_ to continue */
769 WRITELONG (p
, *(long*)data
- realbytes
);
770 elf_sect_write (s
, mydata
, 4L);
774 static void elf_write(void)
776 int nsections
, align
;
783 long symtablen
, symtablocal
;
786 * Work out how many sections we will have. We have SHN_UNDEF,
787 * then the flexible user sections, then the four fixed
788 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
789 * then optionally relocation sections for the user sections.
791 nsections
= 5; /* SHN_UNDEF and the fixed ones */
792 add_sectname ("", ".comment");
793 add_sectname ("", ".shstrtab");
794 add_sectname ("", ".symtab");
795 add_sectname ("", ".strtab");
796 for (i
=0; i
<nsects
; i
++) {
797 nsections
++; /* for the section itself */
798 if (sects
[i
]->head
) {
799 nsections
++; /* for its relocations */
800 add_sectname (".rel", sects
[i
]->name
);
808 commlen
= 2+sprintf(comment
+1, "The Netwide Assembler %s", NASM_VER
);
811 * Output the ELF header.
813 fwrite ("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
814 fwriteshort (1, elffp
); /* ET_REL relocatable file */
815 fwriteshort (3, elffp
); /* EM_386 processor ID */
816 fwritelong (1L, elffp
); /* EV_CURRENT file format version */
817 fwritelong (0L, elffp
); /* no entry point */
818 fwritelong (0L, elffp
); /* no program header table */
819 fwritelong (0x40L
, elffp
); /* section headers straight after
820 * ELF header plus alignment */
821 fwritelong (0L, elffp
); /* 386 defines no special flags */
822 fwriteshort (0x34, elffp
); /* size of ELF header */
823 fwriteshort (0, elffp
); /* no program header table, again */
824 fwriteshort (0, elffp
); /* still no program header table */
825 fwriteshort (0x28, elffp
); /* size of section header */
826 fwriteshort (nsections
, elffp
); /* number of sections */
827 fwriteshort (nsects
+2, elffp
); /* string table section index for
828 * section header table */
829 fwritelong (0L, elffp
); /* align to 0x40 bytes */
830 fwritelong (0L, elffp
);
831 fwritelong (0L, elffp
);
834 * Build the symbol table and relocation tables.
836 symtab
= elf_build_symtab (&symtablen
, &symtablocal
);
837 for (i
=0; i
<nsects
; i
++)
839 sects
[i
]->rel
= elf_build_reltab (§s
[i
]->rellen
,
843 * Now output the section header table.
846 elf_foffs
= 0x40 + 0x28 * nsections
;
847 align
= ((elf_foffs
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
850 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
852 elf_section_header (0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
854 for (i
=0; i
<nsects
; i
++) {
855 elf_section_header (p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
856 (sects
[i
]->type
== SHT_PROGBITS
?
857 sects
[i
]->data
: NULL
), TRUE
,
858 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
861 elf_section_header (p
- shstrtab
, 1, 0, comment
, FALSE
,
862 (long)commlen
, 0, 0, 1, 0);/* .comment */
864 elf_section_header (p
- shstrtab
, 3, 0, shstrtab
, FALSE
,
865 (long)shstrtablen
, 0, 0, 1, 0);/* .shstrtab */
867 elf_section_header (p
- shstrtab
, 2, 0, symtab
, TRUE
,
868 symtablen
, nsects
+4, symtablocal
, 4, 16);/* .symtab */
870 elf_section_header (p
- shstrtab
, 3, 0, strs
, TRUE
,
871 strslen
, 0, 0, 1, 0); /* .strtab */
872 for (i
=0; i
<nsects
; i
++) if (sects
[i
]->head
) {
874 elf_section_header (p
- shstrtab
, 9, 0, sects
[i
]->rel
, TRUE
,
875 sects
[i
]->rellen
, nsects
+3, i
+1, 4, 8);
878 fwrite (align_str
, align
, 1, elffp
);
881 * Now output the sections.
883 elf_write_sections();
885 nasm_free (elf_sects
);
889 static struct SAA
*elf_build_symtab (long *len
, long *local
)
891 struct SAA
*s
= saa_init(1L);
893 unsigned char entry
[16], *p
;
899 * First, an all-zeros entry, required by the ELF spec.
901 saa_wbytes (s
, NULL
, 16L); /* null symbol table entry */
906 * Next, an entry for the file name.
909 WRITELONG (p
, 1); /* we know it's 1st thing in strtab */
910 WRITELONG (p
, 0); /* no value */
911 WRITELONG (p
, 0); /* no size either */
912 WRITESHORT (p
, 4); /* type FILE */
913 WRITESHORT (p
, SHN_ABS
);
914 saa_wbytes (s
, entry
, 16L);
919 * Now some standard symbols defining the segments, for relocation
922 for (i
= 1; i
<= nsects
+1; i
++) {
924 WRITELONG (p
, 0); /* no symbol name */
925 WRITELONG (p
, 0); /* offset zero */
926 WRITELONG (p
, 0); /* size zero */
927 WRITESHORT (p
, 3); /* local section-type thing */
928 WRITESHORT (p
, (i
==1 ? SHN_ABS
: i
-1)); /* the section id */
929 saa_wbytes (s
, entry
, 16L);
935 * Now the other local symbols.
938 while ( (sym
= saa_rstruct (syms
)) != NULL
) {
939 if (sym
->type
& SYM_GLOBAL
)
942 WRITELONG (p
, sym
->strpos
);
943 WRITELONG (p
, sym
->value
);
944 WRITELONG (p
, sym
->size
);
945 WRITESHORT (p
, sym
->type
); /* local non-typed thing */
946 WRITESHORT (p
, sym
->section
);
947 saa_wbytes (s
, entry
, 16L);
953 * Now the global symbols.
956 while ( (sym
= saa_rstruct (syms
)) != NULL
) {
957 if (!(sym
->type
& SYM_GLOBAL
))
960 WRITELONG (p
, sym
->strpos
);
961 WRITELONG (p
, sym
->value
);
962 WRITELONG (p
, sym
->size
);
963 WRITESHORT (p
, sym
->type
); /* global non-typed thing */
964 WRITESHORT (p
, sym
->section
);
965 saa_wbytes (s
, entry
, 16L);
972 static struct SAA
*elf_build_reltab (long *len
, struct Reloc
*r
) {
974 unsigned char *p
, entry
[8];
983 long sym
= r
->symbol
;
985 if (sym
>= GLOBAL_TEMP_BASE
)
986 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+3) + nlocals
;
989 WRITELONG (p
, r
->address
);
990 WRITELONG (p
, (sym
<< 8) + r
->type
);
991 saa_wbytes (s
, entry
, 8L);
1000 static void elf_section_header (int name
, int type
, int flags
,
1001 void *data
, int is_saa
, long datalen
,
1002 int link
, int info
, int align
, int eltsize
)
1004 elf_sects
[elf_nsect
].data
= data
;
1005 elf_sects
[elf_nsect
].len
= datalen
;
1006 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1009 fwritelong ((long)name
, elffp
);
1010 fwritelong ((long)type
, elffp
);
1011 fwritelong ((long)flags
, elffp
);
1012 fwritelong (0L, elffp
); /* no address, ever, in object files */
1013 fwritelong (type
== 0 ? 0L : elf_foffs
, elffp
);
1014 fwritelong (datalen
, elffp
);
1016 elf_foffs
+= (datalen
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1017 fwritelong ((long)link
, elffp
);
1018 fwritelong ((long)info
, elffp
);
1019 fwritelong ((long)align
, elffp
);
1020 fwritelong ((long)eltsize
, elffp
);
1023 static void elf_write_sections (void)
1026 for (i
= 0; i
< elf_nsect
; i
++)
1027 if (elf_sects
[i
].data
) {
1028 long len
= elf_sects
[i
].len
;
1029 long reallen
= (len
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1030 long align
= reallen
- len
;
1031 if (elf_sects
[i
].is_saa
)
1032 saa_fpwrite (elf_sects
[i
].data
, elffp
);
1034 fwrite (elf_sects
[i
].data
, len
, 1, elffp
);
1035 fwrite (align_str
, align
, 1, elffp
);
1039 static void elf_sect_write (struct Section
*sect
,
1040 unsigned char *data
, unsigned long len
)
1042 saa_wbytes (sect
->data
, data
, len
);
1046 static long elf_segbase (long segment
)
1051 static int elf_directive (char *directive
, char *value
, int pass
)
1056 static void elf_filename (char *inname
, char *outname
, efunc error
)
1058 strcpy(elf_module
, inname
);
1059 standard_extension (inname
, outname
, ".o", error
);
1062 static char *elf_stdmac
[] = {
1063 "%define __SECT__ [section .text]",
1064 "%macro __NASM_CDecl__ 1",
1069 static int elf_set_info(enum geninfo type
, char **val
)
1074 struct ofmt of_elf
= {
1075 "ELF32 (i386) object files (e.g. Linux)",