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 16 /* 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
*, const 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 /* this stuff is needed for the stabs debugging format */
146 #define N_SO 0x64 /* ID for main source file */
147 #define N_SOL 0x84 /* ID for sub-source file */
151 #define TY_STABSSYMLIN 0x40 /* ouch */
154 unsigned long n_strx
;
155 unsigned char n_type
;
156 unsigned char n_other
;
157 unsigned short n_desc
;
158 unsigned long n_value
;
167 int section
; /* section index */
168 char *name
; /* shallow-copied pointer of section name */
172 struct symlininfo info
;
175 struct linelist
* next
;
176 struct linelist
* last
;
179 static struct linelist
*stabslines
=0;
180 static int stabs_immcall
=0;
181 static int currentline
=0;
182 static int numlinestabs
=0;
183 static char * stabs_filename
=0;
184 static int symtabsection
;
185 static unsigned char *stabbuf
=0,*stabstrbuf
=0,*stabrelbuf
=0;
186 static int stablen
,stabstrlen
,stabrellen
;
189 void stabs_init(struct ofmt
*,void *,FILE *,efunc
);
190 void stabs_linenum(const char *filename
,long linenumber
,long);
191 void stabs_deflabel(char *,long ,long ,int ,char *);
192 void stabs_directive(const char *,const char *);
193 void stabs_typevalue(long );
194 void stabs_output(int ,void *);
195 void stabs_generate();
196 void stabs_cleanup();
198 /* end of stabs debugging stuff */
202 * Special section numbers which are used to define ELF special
203 * symbols, which can be used with WRT to provide PIC relocation
206 static long elf_gotpc_sect
, elf_gotoff_sect
;
207 static long elf_got_sect
, elf_plt_sect
;
208 static long elf_sym_sect
;
210 static void elf_init(FILE *fp
, efunc errfunc
, ldfunc ldef
, evalfunc eval
)
215 (void) ldef
; /* placate optimisers */
217 nsects
= sectlen
= 0;
218 syms
= saa_init((long)sizeof(struct Symbol
));
219 nlocals
= nglobs
= 0;
222 saa_wbytes (strs
, "\0", 1L);
223 saa_wbytes (strs
, elf_module
, (long)(strlen(elf_module
)+1));
224 strslen
= 2+strlen(elf_module
);
226 shstrtablen
= shstrtabsize
= 0;;
227 add_sectname ("", "");
231 elf_gotpc_sect
= seg_alloc();
232 ldef("..gotpc", elf_gotpc_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
233 elf_gotoff_sect
= seg_alloc();
234 ldef("..gotoff", elf_gotoff_sect
+1, 0L, NULL
, FALSE
, FALSE
,&of_elf
,error
);
235 elf_got_sect
= seg_alloc();
236 ldef("..got", elf_got_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
237 elf_plt_sect
= seg_alloc();
238 ldef("..plt", elf_plt_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
239 elf_sym_sect
= seg_alloc();
240 ldef("..sym", elf_sym_sect
+1, 0L, NULL
, FALSE
, FALSE
, &of_elf
, error
);
242 def_seg
= seg_alloc();
245 static void elf_cleanup(int debuginfo
)
254 for (i
=0; i
<nsects
; i
++) {
255 if (sects
[i
]->type
!= SHT_NOBITS
)
256 saa_free (sects
[i
]->data
);
258 saa_free (sects
[i
]->rel
);
259 while (sects
[i
]->head
) {
261 sects
[i
]->head
= sects
[i
]->head
->next
;
269 if (of_elf
.current_dfmt
) {
270 of_elf
.current_dfmt
->cleanup();
274 static void add_sectname (char *firsthalf
, char *secondhalf
)
276 int len
= strlen(firsthalf
)+strlen(secondhalf
);
277 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
278 shstrtab
= nasm_realloc (shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
279 strcpy (shstrtab
+shstrtablen
, firsthalf
);
280 strcat (shstrtab
+shstrtablen
, secondhalf
);
281 shstrtablen
+= len
+1;
284 static int elf_make_section (char *name
, int type
, int flags
, int align
)
288 s
= nasm_malloc (sizeof(*s
));
290 if (type
!= SHT_NOBITS
)
291 s
->data
= saa_init (1L);
294 s
->len
= s
->size
= 0;
296 if (!strcmp(name
, ".text"))
299 s
->index
= seg_alloc();
300 add_sectname ("", name
);
301 s
->name
= nasm_malloc (1+strlen(name
));
302 strcpy (s
->name
, name
);
308 if (nsects
>= sectlen
)
309 sects
= nasm_realloc (sects
, (sectlen
+= SECT_DELTA
)*sizeof(*sects
));
315 static long elf_section_names (char *name
, int pass
, int *bits
)
318 int flags_and
, flags_or
, type
, align
, i
;
321 * Default is 32 bits.
329 while (*p
&& !isspace(*p
)) p
++;
331 flags_and
= flags_or
= type
= align
= 0;
333 while (*p
&& isspace(*p
)) p
++;
336 while (*p
&& !isspace(*p
)) p
++;
338 while (*p
&& isspace(*p
)) p
++;
340 if (!nasm_strnicmp(q
, "align=", 6)) {
344 if ( (align
-1) & align
) { /* means it's not a power of two */
345 error (ERR_NONFATAL
, "section alignment %d is not"
346 " a power of two", align
);
349 } else if (!nasm_stricmp(q
, "alloc")) {
350 flags_and
|= SHF_ALLOC
;
351 flags_or
|= SHF_ALLOC
;
352 } else if (!nasm_stricmp(q
, "noalloc")) {
353 flags_and
|= SHF_ALLOC
;
354 flags_or
&= ~SHF_ALLOC
;
355 } else if (!nasm_stricmp(q
, "exec")) {
356 flags_and
|= SHF_EXECINSTR
;
357 flags_or
|= SHF_EXECINSTR
;
358 } else if (!nasm_stricmp(q
, "noexec")) {
359 flags_and
|= SHF_EXECINSTR
;
360 flags_or
&= ~SHF_EXECINSTR
;
361 } else if (!nasm_stricmp(q
, "write")) {
362 flags_and
|= SHF_WRITE
;
363 flags_or
|= SHF_WRITE
;
364 } else if (!nasm_stricmp(q
, "nowrite")) {
365 flags_and
|= SHF_WRITE
;
366 flags_or
&= ~SHF_WRITE
;
367 } else if (!nasm_stricmp(q
, "progbits")) {
369 } else if (!nasm_stricmp(q
, "nobits")) {
374 if (!strcmp(name
, ".comment") ||
375 !strcmp(name
, ".shstrtab") ||
376 !strcmp(name
, ".symtab") ||
377 !strcmp(name
, ".strtab")) {
378 error (ERR_NONFATAL
, "attempt to redefine reserved section"
383 for (i
=0; i
<nsects
; i
++)
384 if (!strcmp(name
, sects
[i
]->name
))
387 if (!strcmp(name
, ".text"))
388 i
= elf_make_section (name
, SHT_PROGBITS
,
389 SHF_ALLOC
| SHF_EXECINSTR
, 16);
390 else if (!strcmp(name
, ".rodata"))
391 i
= elf_make_section (name
, SHT_PROGBITS
,
393 else if (!strcmp(name
, ".data"))
394 i
= elf_make_section (name
, SHT_PROGBITS
,
395 SHF_ALLOC
| SHF_WRITE
, 4);
396 else if (!strcmp(name
, ".bss"))
397 i
= elf_make_section (name
, SHT_NOBITS
,
398 SHF_ALLOC
| SHF_WRITE
, 4);
400 i
= elf_make_section (name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
402 sects
[i
]->type
= type
;
404 sects
[i
]->align
= align
;
405 sects
[i
]->flags
&= ~flags_and
;
406 sects
[i
]->flags
|= flags_or
;
407 } else if (pass
== 1) {
408 if (type
|| align
|| flags_and
)
409 error (ERR_WARNING
, "section attributes ignored on"
410 " redeclaration of section `%s'", name
);
413 return sects
[i
]->index
;
416 static void elf_deflabel (char *name
, long segment
, long offset
,
417 int is_global
, char *special
)
421 int special_used
= FALSE
;
423 #if defined(DEBUG) && DEBUG>2
424 fprintf(stderr
, " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
425 name
, segment
, offset
, is_global
, special
);
427 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
429 * This is a NASM special symbol. We never allow it into
430 * the ELF symbol table, even if it's a valid one. If it
431 * _isn't_ a valid one, we should barf immediately.
433 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
434 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
435 strcmp(name
, "..sym"))
436 error (ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
440 if (is_global
== 3) {
443 * Fix up a forward-reference symbol size from the first
446 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
447 if (!strcmp((*s
)->name
, name
)) {
448 struct tokenval tokval
;
452 while (*p
&& !isspace(*p
)) p
++;
453 while (*p
&& isspace(*p
)) p
++;
456 tokval
.t_type
= TOKEN_INVALID
;
457 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
460 error (ERR_NONFATAL
, "cannot use relocatable"
461 " expression as symbol size");
463 (*s
)->size
= reloc_value(e
);
467 * Remove it from the list of unresolved sizes.
469 nasm_free ((*s
)->name
);
473 return; /* it wasn't an important one */
476 saa_wbytes (strs
, name
, (long)(1+strlen(name
)));
477 strslen
+= 1+strlen(name
);
479 sym
= saa_wstruct (syms
);
482 sym
->type
= is_global
? SYM_GLOBAL
: 0;
484 if (segment
== NO_SEG
)
485 sym
->section
= SHN_ABS
;
488 sym
->section
= SHN_UNDEF
;
489 if (nsects
== 0 && segment
== def_seg
) {
491 if (segment
!= elf_section_names (".text", 2, &tempint
))
492 error (ERR_PANIC
, "strange segment conditions in ELF driver");
493 sym
->section
= nsects
;
495 for (i
=0; i
<nsects
; i
++)
496 if (segment
== sects
[i
]->index
) {
503 if (is_global
== 2) {
506 sym
->section
= SHN_COMMON
;
508 * We have a common variable. Check the special text to see
509 * if it's a valid number and power of two; if so, store it
510 * as the alignment for the common variable.
514 sym
->value
= readnum (special
, &err
);
516 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
517 " valid number", special
);
518 else if ( (sym
->value
| (sym
->value
-1)) != 2*sym
->value
- 1)
519 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
520 " power of two", special
);
524 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
526 if (sym
->type
== SYM_GLOBAL
) {
528 * There's a problem here that needs fixing.
529 * If sym->section == SHN_ABS, then the first line of the
530 * else section causes a core dump, because its a reference
531 * beyond the end of the section array.
532 * This behaviour is exhibited by this code:
536 * I'm not sure how to procede, because I haven't got the
537 * first clue about how ELF works, so I don't know what to
538 * do with it. Furthermore, I'm not sure what the rest of this
539 * section of code does. Help?
541 * For now, I'll see if doing absolutely nothing with it will
544 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
)
546 bsym
= raa_write (bsym
, segment
, nglobs
);
548 else if (sym
->section
!= SHN_ABS
)
551 * This is a global symbol; so we must add it to the linked
552 * list of global symbols in its section. We'll push it on
553 * the beginning of the list, because it doesn't matter
554 * much which end we put it on and it's easier like this.
556 * In addition, we check the special text for symbol
557 * type and size information.
559 sym
->next
= sects
[sym
->section
-1]->gsyms
;
560 sects
[sym
->section
-1]->gsyms
= sym
;
563 int n
= strcspn(special
, " ");
565 if (!nasm_strnicmp(special
, "function", n
))
566 sym
->type
|= SYM_FUNCTION
;
567 else if (!nasm_strnicmp(special
, "data", n
) ||
568 !nasm_strnicmp(special
, "object", n
))
569 sym
->type
|= SYM_DATA
;
571 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
574 struct tokenval tokval
;
577 char *saveme
=stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
579 while (special
[n
] && isspace(special
[n
]))
582 * We have a size expression; attempt to
586 stdscan_bufptr
= special
+n
;
587 tokval
.t_type
= TOKEN_INVALID
;
588 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
, NULL
);
592 sym
->name
= nasm_strdup(name
);
595 error (ERR_NONFATAL
, "cannot use relocatable"
596 " expression as symbol size");
598 sym
->size
= reloc_value(e
);
600 stdscan_bufptr
=saveme
; /* bugfix? fbk 8/10/00 */
605 sym
->globnum
= nglobs
;
610 if (special
&& !special_used
)
611 error(ERR_NONFATAL
, "no special symbol features supported here");
614 static void elf_add_reloc (struct Section
*sect
, long segment
,
619 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
620 sect
->tail
= &r
->next
;
623 r
->address
= sect
->len
;
624 if (segment
== NO_SEG
)
629 for (i
=0; i
<nsects
; i
++)
630 if (segment
== sects
[i
]->index
)
633 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
641 * This routine deals with ..got and ..sym relocations: the more
642 * complicated kinds. In shared-library writing, some relocations
643 * with respect to global symbols must refer to the precise symbol
644 * rather than referring to an offset from the base of the section
645 * _containing_ the symbol. Such relocations call to this routine,
646 * which searches the symbol list for the symbol in question.
648 * R_386_GOT32 references require the _exact_ symbol address to be
649 * used; R_386_32 references can be at an offset from the symbol.
650 * The boolean argument `exact' tells us this.
652 * Return value is the adjusted value of `addr', having become an
653 * offset from the symbol rather than the section. Should always be
654 * zero when returning from an exact call.
656 * Limitation: if you define two symbols at the same place,
657 * confusion will occur.
659 * Inefficiency: we search, currently, using a linked list which
660 * isn't even necessarily sorted.
662 static long elf_add_gsym_reloc (struct Section
*sect
,
663 long segment
, long offset
,
668 struct Symbol
*sym
, *sm
;
672 * First look up the segment/offset pair and find a global
673 * symbol corresponding to it. If it's not one of our segments,
674 * then it must be an external symbol, in which case we're fine
675 * doing a normal elf_add_reloc after first sanity-checking
676 * that the offset from the symbol is zero.
679 for (i
=0; i
<nsects
; i
++)
680 if (segment
== sects
[i
]->index
) {
685 if (exact
&& offset
!= 0)
686 error (ERR_NONFATAL
, "unable to find a suitable global symbol"
687 " for this reference");
689 elf_add_reloc (sect
, segment
, type
);
695 * Find a symbol pointing _exactly_ at this one.
697 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
698 if (sym
->value
== offset
)
702 * Find the nearest symbol below this one.
705 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
706 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
710 error (ERR_NONFATAL
, "unable to find a suitable global symbol"
711 " for this reference");
715 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
716 sect
->tail
= &r
->next
;
719 r
->address
= sect
->len
;
720 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
725 return offset
- sym
->value
;
728 static void elf_out (long segto
, const void *data
, unsigned long type
,
729 long segment
, long wrt
)
732 long realbytes
= type
& OUT_SIZMASK
;
734 unsigned char mydata
[4], *p
;
736 static struct symlininfo sinfo
;
741 * handle absolute-assembly (structure definitions)
743 if (segto
== NO_SEG
) {
744 if (type
!= OUT_RESERVE
)
745 error (ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
751 for (i
=0; i
<nsects
; i
++)
752 if (segto
== sects
[i
]->index
) {
757 int tempint
; /* ignored */
758 if (segto
!= elf_section_names (".text", 2, &tempint
))
759 error (ERR_PANIC
, "strange segment conditions in ELF driver");
766 /* again some stabs debugging stuff */
767 if (of_elf
.current_dfmt
) {
771 of_elf
.current_dfmt
->debug_output(TY_STABSSYMLIN
,&sinfo
);
773 /* end of debugging stuff */
775 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
776 error(ERR_WARNING
, "attempt to initialise memory in"
777 " BSS section `%s': ignored", s
->name
);
778 if (type
== OUT_REL2ADR
)
780 else if (type
== OUT_REL4ADR
)
786 if (type
== OUT_RESERVE
) {
787 if (s
->type
== SHT_PROGBITS
) {
788 error(ERR_WARNING
, "uninitialised space declared in"
789 " non-BSS section `%s': zeroing", s
->name
);
790 elf_sect_write (s
, NULL
, realbytes
);
793 } else if (type
== OUT_RAWDATA
) {
794 if (segment
!= NO_SEG
)
795 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
796 elf_sect_write (s
, data
, realbytes
);
797 } else if (type
== OUT_ADDRESS
) {
799 addr
= *(long *)data
;
800 if (segment
!= NO_SEG
) {
802 error(ERR_NONFATAL
, "ELF format does not support"
803 " segment base references");
806 if ( realbytes
== 2 ) {
808 elf_add_reloc (s
, segment
, R_386_16
);
810 elf_add_reloc (s
, segment
, R_386_32
);
812 } else if (wrt
== elf_gotpc_sect
+1) {
814 * The user will supply GOT relative to $$. ELF
815 * will let us have GOT relative to $. So we
816 * need to fix up the data item by $-$$.
819 elf_add_reloc (s
, segment
, R_386_GOTPC
);
820 } else if (wrt
== elf_gotoff_sect
+1) {
821 elf_add_reloc (s
, segment
, R_386_GOTOFF
);
822 } else if (wrt
== elf_got_sect
+1) {
823 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
825 } else if (wrt
== elf_sym_sect
+1) {
826 if ( realbytes
== 2 ) {
828 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
831 addr
= elf_add_gsym_reloc (s
, segment
, addr
,
834 } else if (wrt
== elf_plt_sect
+1) {
835 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
836 "relative PLT references");
838 error (ERR_NONFATAL
, "ELF format does not support this"
840 wrt
= NO_SEG
; /* we can at least _try_ to continue */
846 error(ERR_WARNING
|ERR_WARN_GNUELF
,
847 "16-bit relocations in ELF is a GNU extension");
848 WRITESHORT (p
, addr
);
850 if (realbytes
!= 4 && segment
!= NO_SEG
) {
851 error (ERR_NONFATAL
, "Unsupported non-32-bit ELF relocation");
855 elf_sect_write (s
, mydata
, realbytes
);
856 } else if (type
== OUT_REL2ADR
) {
857 if (segment
== segto
)
858 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
859 if (segment
!= NO_SEG
&& segment
% 2) {
860 error(ERR_NONFATAL
, "ELF format does not support"
861 " segment base references");
864 error (ERR_WARNING
|ERR_WARN_GNUELF
,
865 "16-bit relocations in ELF is a GNU extension");
866 elf_add_reloc (s
, segment
, R_386_PC16
);
868 error (ERR_NONFATAL
, "Unsupported non-32-bit ELF relocation");
872 WRITESHORT (p
, *(long*)data
- realbytes
);
873 elf_sect_write (s
, mydata
, 2L);
874 } else if (type
== OUT_REL4ADR
) {
875 if (segment
== segto
)
876 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
877 if (segment
!= NO_SEG
&& segment
% 2) {
878 error(ERR_NONFATAL
, "ELF format does not support"
879 " segment base references");
882 elf_add_reloc (s
, segment
, R_386_PC32
);
883 } else if (wrt
== elf_plt_sect
+1) {
884 elf_add_reloc (s
, segment
, R_386_PLT32
);
885 } else if (wrt
== elf_gotpc_sect
+1 ||
886 wrt
== elf_gotoff_sect
+1 ||
887 wrt
== elf_got_sect
+1) {
888 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
889 "relative GOT references");
891 error (ERR_NONFATAL
, "ELF format does not support this"
893 wrt
= NO_SEG
; /* we can at least _try_ to continue */
897 WRITELONG (p
, *(long*)data
- realbytes
);
898 elf_sect_write (s
, mydata
, 4L);
902 static void elf_write(void)
904 int nsections
, align
;
912 long symtablen
, symtablocal
;
915 * Work out how many sections we will have. We have SHN_UNDEF,
916 * then the flexible user sections, then the four fixed
917 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
918 * then optionally relocation sections for the user sections.
920 if (of_elf
.current_dfmt
)
923 nsections
= 5; /* SHN_UNDEF and the fixed ones */
925 add_sectname ("", ".comment");
926 add_sectname ("", ".shstrtab");
927 add_sectname ("", ".symtab");
928 add_sectname ("", ".strtab");
929 for (i
=0; i
<nsects
; i
++) {
930 nsections
++; /* for the section itself */
931 if (sects
[i
]->head
) {
932 nsections
++; /* for its relocations */
933 add_sectname (".rel", sects
[i
]->name
);
937 if (of_elf
.current_dfmt
) {
938 /* in case the debug information is wanted, just add these three sections... */
939 add_sectname("",".stab");
940 add_sectname("",".stabstr");
941 add_sectname(".rel",".stab");
948 commlen
= 2+sprintf(comment
+1, "The Netwide Assembler %s", NASM_VER
);
951 * Output the ELF header.
953 fwrite ("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
954 fwriteshort (1, elffp
); /* ET_REL relocatable file */
955 fwriteshort (3, elffp
); /* EM_386 processor ID */
956 fwritelong (1L, elffp
); /* EV_CURRENT file format version */
957 fwritelong (0L, elffp
); /* no entry point */
958 fwritelong (0L, elffp
); /* no program header table */
959 fwritelong (0x40L
, elffp
); /* section headers straight after
960 * ELF header plus alignment */
961 fwritelong (0L, elffp
); /* 386 defines no special flags */
962 fwriteshort (0x34, elffp
); /* size of ELF header */
963 fwriteshort (0, elffp
); /* no program header table, again */
964 fwriteshort (0, elffp
); /* still no program header table */
965 fwriteshort (0x28, elffp
); /* size of section header */
966 fwriteshort (nsections
, elffp
); /* number of sections */
967 fwriteshort (nsects
+2, elffp
); /* string table section index for
968 * section header table */
969 fwritelong (0L, elffp
); /* align to 0x40 bytes */
970 fwritelong (0L, elffp
);
971 fwritelong (0L, elffp
);
974 * Build the symbol table and relocation tables.
976 symtab
= elf_build_symtab (&symtablen
, &symtablocal
);
977 for (i
=0; i
<nsects
; i
++)
979 sects
[i
]->rel
= elf_build_reltab (§s
[i
]->rellen
,
983 * Now output the section header table.
986 elf_foffs
= 0x40 + 0x28 * nsections
;
987 align
= ((elf_foffs
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
990 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
992 elf_section_header (0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
993 scount
=1; /* needed for the stabs debugging to track the symtable section */
995 for (i
=0; i
<nsects
; i
++) {
996 elf_section_header (p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
997 (sects
[i
]->type
== SHT_PROGBITS
?
998 sects
[i
]->data
: NULL
), TRUE
,
999 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1001 scount
++; /* dito */
1003 elf_section_header (p
- shstrtab
, 1, 0, comment
, FALSE
,
1004 (long)commlen
, 0, 0, 1, 0);/* .comment */
1005 scount
++; /* dito */
1007 elf_section_header (p
- shstrtab
, 3, 0, shstrtab
, FALSE
,
1008 (long)shstrtablen
, 0, 0, 1, 0);/* .shstrtab */
1009 scount
++; /* dito */
1011 elf_section_header (p
- shstrtab
, 2, 0, symtab
, TRUE
,
1012 symtablen
, nsects
+4, symtablocal
, 4, 16);/* .symtab */
1013 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1015 elf_section_header (p
- shstrtab
, 3, 0, strs
, TRUE
,
1016 strslen
, 0, 0, 1, 0); /* .strtab */
1017 for (i
=0; i
<nsects
; i
++) if (sects
[i
]->head
) {
1019 elf_section_header (p
- shstrtab
, 9, 0, sects
[i
]->rel
, TRUE
,
1020 sects
[i
]->rellen
, nsects
+3, i
+1, 4, 8);
1022 if (of_elf
.current_dfmt
) {
1023 /* for debugging information, create the last three sections
1024 which are the .stab , .stabstr and .rel.stab sections respectively */
1026 /* this function call creates the stab sections in memory */
1029 if ((stabbuf
)&&(stabstrbuf
)&&(stabrelbuf
)) {
1031 elf_section_header(p
-shstrtab
,1,0,stabbuf
,0,stablen
,nsections
-2,0,4,12);
1034 elf_section_header(p
-shstrtab
,3,0,stabstrbuf
,0,stabstrlen
,0,0,4,0);
1037 /* link -> symtable info -> section to refer to */
1038 elf_section_header(p
-shstrtab
,9,0,stabrelbuf
,0,stabrellen
,symtabsection
,nsections
-3,4,8);
1041 fwrite (align_str
, align
, 1, elffp
);
1044 * Now output the sections.
1046 elf_write_sections();
1048 nasm_free (elf_sects
);
1052 static struct SAA
*elf_build_symtab (long *len
, long *local
)
1054 struct SAA
*s
= saa_init(1L);
1056 unsigned char entry
[16], *p
;
1062 * First, an all-zeros entry, required by the ELF spec.
1064 saa_wbytes (s
, NULL
, 16L); /* null symbol table entry */
1069 * Next, an entry for the file name.
1072 WRITELONG (p
, 1); /* we know it's 1st thing in strtab */
1073 WRITELONG (p
, 0); /* no value */
1074 WRITELONG (p
, 0); /* no size either */
1075 WRITESHORT (p
, 4); /* type FILE */
1076 WRITESHORT (p
, SHN_ABS
);
1077 saa_wbytes (s
, entry
, 16L);
1082 * Now some standard symbols defining the segments, for relocation
1085 for (i
= 1; i
<= nsects
+1; i
++) {
1087 WRITELONG (p
, 0); /* no symbol name */
1088 WRITELONG (p
, 0); /* offset zero */
1089 WRITELONG (p
, 0); /* size zero */
1090 WRITESHORT (p
, 3); /* local section-type thing */
1091 WRITESHORT (p
, (i
==1 ? SHN_ABS
: i
-1)); /* the section id */
1092 saa_wbytes (s
, entry
, 16L);
1098 * Now the other local symbols.
1101 while ( (sym
= saa_rstruct (syms
)) ) {
1102 if (sym
->type
& SYM_GLOBAL
)
1105 WRITELONG (p
, sym
->strpos
);
1106 WRITELONG (p
, sym
->value
);
1107 WRITELONG (p
, sym
->size
);
1108 WRITESHORT (p
, sym
->type
); /* local non-typed thing */
1109 WRITESHORT (p
, sym
->section
);
1110 saa_wbytes (s
, entry
, 16L);
1116 * Now the global symbols.
1119 while ( (sym
= saa_rstruct (syms
)) ) {
1120 if (!(sym
->type
& SYM_GLOBAL
))
1123 WRITELONG (p
, sym
->strpos
);
1124 WRITELONG (p
, sym
->value
);
1125 WRITELONG (p
, sym
->size
);
1126 WRITESHORT (p
, sym
->type
); /* global non-typed thing */
1127 WRITESHORT (p
, sym
->section
);
1128 saa_wbytes (s
, entry
, 16L);
1135 static struct SAA
*elf_build_reltab (long *len
, struct Reloc
*r
) {
1137 unsigned char *p
, entry
[8];
1146 long sym
= r
->symbol
;
1148 if (sym
>= GLOBAL_TEMP_BASE
)
1149 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+3) + nlocals
;
1152 WRITELONG (p
, r
->address
);
1153 WRITELONG (p
, (sym
<< 8) + r
->type
);
1154 saa_wbytes (s
, entry
, 8L);
1163 static void elf_section_header (int name
, int type
, int flags
,
1164 void *data
, int is_saa
, long datalen
,
1165 int link
, int info
, int align
, int eltsize
)
1167 elf_sects
[elf_nsect
].data
= data
;
1168 elf_sects
[elf_nsect
].len
= datalen
;
1169 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1172 fwritelong ((long)name
, elffp
);
1173 fwritelong ((long)type
, elffp
);
1174 fwritelong ((long)flags
, elffp
);
1175 fwritelong (0L, elffp
); /* no address, ever, in object files */
1176 fwritelong (type
== 0 ? 0L : elf_foffs
, elffp
);
1177 fwritelong (datalen
, elffp
);
1179 elf_foffs
+= (datalen
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1180 fwritelong ((long)link
, elffp
);
1181 fwritelong ((long)info
, elffp
);
1182 fwritelong ((long)align
, elffp
);
1183 fwritelong ((long)eltsize
, elffp
);
1186 static void elf_write_sections (void)
1189 for (i
= 0; i
< elf_nsect
; i
++)
1190 if (elf_sects
[i
].data
) {
1191 long len
= elf_sects
[i
].len
;
1192 long reallen
= (len
+SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1193 long align
= reallen
- len
;
1194 if (elf_sects
[i
].is_saa
)
1195 saa_fpwrite (elf_sects
[i
].data
, elffp
);
1197 fwrite (elf_sects
[i
].data
, len
, 1, elffp
);
1198 fwrite (align_str
, align
, 1, elffp
);
1202 static void elf_sect_write (struct Section
*sect
,
1203 const unsigned char *data
, unsigned long len
)
1205 saa_wbytes (sect
->data
, data
, len
);
1209 static long elf_segbase (long segment
)
1214 static int elf_directive (char *directive
, char *value
, int pass
)
1219 static void elf_filename (char *inname
, char *outname
, efunc error
)
1221 strcpy(elf_module
, inname
);
1222 standard_extension (inname
, outname
, ".o", error
);
1225 static const char *elf_stdmac
[] = {
1226 "%define __SECT__ [section .text]",
1227 "%macro __NASM_CDecl__ 1",
1232 static int elf_set_info(enum geninfo type
, char **val
)
1237 static struct dfmt df_stabs
= {
1238 "ELF32 (i386) stabs debug format for Linux",
1249 struct dfmt
*elf_debugs_arr
[2]={&df_stabs
,NULL
};
1251 struct ofmt of_elf
= {
1252 "ELF32 (i386) object files (e.g. Linux)",
1269 /* again, the stabs debugging stuff (code) */
1271 void stabs_init(struct ofmt
*of
,void *id
,FILE *fp
,efunc error
) {
1274 void stabs_linenum(const char *filename
,long linenumber
,long segto
) {
1275 if (!stabs_filename
) {
1276 stabs_filename
= (char *)malloc(strlen(filename
)+1);
1277 strcpy(stabs_filename
,filename
);
1279 if (strcmp(stabs_filename
,filename
)) {
1280 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1281 in fact, this leak comes in quite handy to maintain a list of files
1282 encountered so far in the symbol lines... */
1283 stabs_filename
= (char *)malloc(strlen(filename
)+1);
1284 strcpy(stabs_filename
,filename
);
1288 currentline
=linenumber
;
1291 void stabs_deflabel(char *name
,long segment
,long offset
,int is_global
,char *special
) {
1294 void stabs_directive(const char *directive
,const char *params
) {
1297 void stabs_typevalue(long type
) {
1300 void stabs_output(int type
,void *param
) {
1301 struct symlininfo
*s
;
1302 struct linelist
*el
;
1303 if (type
==TY_STABSSYMLIN
) {
1304 if (stabs_immcall
) {
1305 s
=(struct symlininfo
*)param
;
1306 if (strcmp( s
->name
,".text")) return; /* we are only interested in the text stuff */
1308 el
=(struct linelist
*)malloc(sizeof(struct linelist
));
1309 el
->info
.offset
=s
->offset
;
1310 el
->info
.section
=s
->section
;
1311 el
->info
.name
=s
->name
;
1312 el
->line
=currentline
;
1313 el
->filename
=stabs_filename
;
1316 stabslines
->last
->next
=el
;
1317 stabslines
->last
=el
;
1320 stabslines
->last
=el
;
1328 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1330 void stabs_generate() {
1331 int i
,numfiles
,strsize
,numstabs
=0,currfile
,mainfileindex
;
1333 unsigned char *sbuf
,*ssbuf
,*rbuf
,*sptr
,*rptr
;
1337 struct linelist
*ptr
;
1338 struct stabentry stab
;
1342 allfiles
= (char **)malloc(numlinestabs
*sizeof(char *));
1343 for (i
=0;i
<numlinestabs
;i
++) allfiles
[i
]=0;
1347 allfiles
[0]=ptr
->filename
;
1350 for (i
=0;i
<numfiles
;i
++) {
1351 if (!strcmp(allfiles
[i
],ptr
->filename
)) break;
1354 allfiles
[i
]=ptr
->filename
;
1361 fileidx
= (int *)malloc(numfiles
*sizeof(int));
1362 for (i
=0;i
<numfiles
;i
++) {
1364 strsize
+=strlen(allfiles
[i
])+1;
1367 for (i
=0;i
<numfiles
;i
++) {
1368 if (!strcmp(allfiles
[i
],elf_module
)) {
1375 /* worst case size of the stab buffer would be:
1376 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1378 sbuf
= (unsigned char *)malloc((numlinestabs
*2+3)*sizeof(struct stabentry
));
1380 ssbuf
= (unsigned char *)malloc(strsize
);
1382 rbuf
= (unsigned char *)malloc(numlinestabs
*8*(2+3));
1385 for (i
=0;i
<numfiles
;i
++) {
1386 strcpy(ssbuf
+fileidx
[i
],allfiles
[i
]);
1390 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1393 /* this is the first stab, its strx points to the filename of the
1394 the source-file, the n_desc field should be set to the number
1397 stab
.n_strx
= fileidx
[0];
1400 stab
.n_desc
=0; /* # of remaining stabs (to be determined later) */
1401 stab
.n_value
= strlen(allfiles
[0])+1;
1402 memcpy(sptr
,&stab
,sizeof(struct stabentry
));
1403 sptr
+=sizeof(struct stabentry
);
1406 for (i
=0;i
<numfiles
;i
++) if (!strcmp(allfiles
[i
],ptr
->filename
)) break;
1409 /* this is the stab for the main source file */
1410 stab
.n_strx
= fileidx
[mainfileindex
];
1414 stab
.n_value
= 0; /* strlen(allfiles[mainfileindex])+1; */
1415 memcpy(sptr
,&stab
,sizeof(struct stabentry
));
1416 /* relocation stuff */
1417 rentry
.offset
=(sptr
-sbuf
)+8;
1418 /* the following section+3 assumption relies on the following order of output sections:
1424 ... this is an ugly hack and should be improved in the near future
1426 rentry
.info
=((ptr
->info
.section
+3)<<8)|R_386_32
;
1427 memcpy(rptr
,&rentry
,8);
1429 sptr
+=sizeof(struct stabentry
);
1432 currfile
=mainfileindex
;
1435 if (strcmp(allfiles
[currfile
],ptr
->filename
)) {
1436 /* oops file has changed... */
1437 for (i
=0;i
<numfiles
;i
++) if (!strcmp(allfiles
[i
],ptr
->filename
)) break;
1439 stab
.n_strx
= fileidx
[currfile
];
1443 /* stab.n_value = strlen(allfiles[currfile])+1; */
1444 stab
.n_value
= ptr
->info
.offset
;
1445 memcpy(sptr
,&stab
,sizeof(struct stabentry
));
1446 /* relocation stuff */
1447 rentry
.offset
=(sptr
-sbuf
)+8;
1448 rentry
.info
=((ptr
->info
.section
+3)<<8)|R_386_32
;
1449 memcpy(rptr
,&rentry
,8);
1451 sptr
+=sizeof(struct stabentry
);
1455 stab
.n_type
=N_SLINE
;
1457 stab
.n_desc
=ptr
->line
;
1458 stab
.n_value
=ptr
->info
.offset
;
1459 memcpy(sptr
,&stab
,sizeof(struct stabentry
));
1462 /* relocation stuff */
1463 rentry
.offset
=(sptr
-sbuf
)+8;
1464 rentry
.info
=((ptr
->info
.section
+3)<<8)|R_386_32
;
1465 memcpy(rptr
,&rentry
,8);
1468 sptr
+=sizeof(struct stabentry
);
1474 ((struct stabentry
*)sbuf
)->n_desc
=numstabs
;
1479 stablen
= (sptr
-sbuf
);
1480 stabrellen
=(rptr
-rbuf
);
1486 void stabs_cleanup() {
1487 struct linelist
*ptr
,*del
;
1488 if (!stabslines
) return;
1495 if (stabbuf
) free(stabbuf
);
1496 if (stabrelbuf
) free(stabrelbuf
);
1497 if (stabstrbuf
) free(stabstrbuf
);