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 *,
138 static void elf_section_header(int, int, int, void *, int, long, int, int,
140 static void elf_write_sections(void);
141 static struct SAA
*elf_build_symtab(long *, long *);
142 static struct SAA
*elf_build_reltab(long *, struct Reloc
*);
143 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
;
188 static struct dfmt df_stabs
;
190 void stabs_init(struct ofmt
*, void *, FILE *, efunc
);
191 void stabs_linenum(const char *filename
, long linenumber
, long);
192 void stabs_deflabel(char *, long, long, int, char *);
193 void stabs_directive(const char *, const char *);
194 void stabs_typevalue(long);
195 void stabs_output(int, void *);
196 void stabs_generate();
197 void stabs_cleanup();
199 /* 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
,
234 elf_gotoff_sect
= seg_alloc();
235 ldef("..gotoff", elf_gotoff_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf
,
237 elf_got_sect
= seg_alloc();
238 ldef("..got", elf_got_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf
,
240 elf_plt_sect
= seg_alloc();
241 ldef("..plt", elf_plt_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf
,
243 elf_sym_sect
= seg_alloc();
244 ldef("..sym", elf_sym_sect
+ 1, 0L, NULL
, FALSE
, FALSE
, &of_elf
,
247 def_seg
= seg_alloc();
250 static void elf_cleanup(int debuginfo
)
259 for (i
= 0; i
< nsects
; i
++) {
260 if (sects
[i
]->type
!= SHT_NOBITS
)
261 saa_free(sects
[i
]->data
);
263 saa_free(sects
[i
]->rel
);
264 while (sects
[i
]->head
) {
266 sects
[i
]->head
= sects
[i
]->head
->next
;
274 if (of_elf
.current_dfmt
) {
275 of_elf
.current_dfmt
->cleanup();
279 static void add_sectname(char *firsthalf
, char *secondhalf
)
281 int len
= strlen(firsthalf
) + strlen(secondhalf
);
282 while (shstrtablen
+ len
+ 1 > shstrtabsize
)
283 shstrtab
= nasm_realloc(shstrtab
, (shstrtabsize
+= SHSTR_DELTA
));
284 strcpy(shstrtab
+ shstrtablen
, firsthalf
);
285 strcat(shstrtab
+ shstrtablen
, secondhalf
);
286 shstrtablen
+= len
+ 1;
289 static int elf_make_section(char *name
, int type
, int flags
, int align
)
293 s
= nasm_malloc(sizeof(*s
));
295 if (type
!= SHT_NOBITS
)
296 s
->data
= saa_init(1L);
299 s
->len
= s
->size
= 0;
301 if (!strcmp(name
, ".text"))
304 s
->index
= seg_alloc();
305 add_sectname("", name
);
306 s
->name
= nasm_malloc(1 + strlen(name
));
307 strcpy(s
->name
, name
);
313 if (nsects
>= sectlen
)
315 nasm_realloc(sects
, (sectlen
+= SECT_DELTA
) * sizeof(*sects
));
321 static long elf_section_names(char *name
, int pass
, int *bits
)
324 int flags_and
, flags_or
, type
, align
, i
;
327 * Default is 32 bits.
335 while (*p
&& !isspace(*p
))
339 flags_and
= flags_or
= type
= align
= 0;
341 while (*p
&& isspace(*p
))
345 while (*p
&& !isspace(*p
))
349 while (*p
&& isspace(*p
))
352 if (!nasm_strnicmp(q
, "align=", 6)) {
356 if ((align
- 1) & align
) { /* means it's not a power of two */
357 error(ERR_NONFATAL
, "section alignment %d is not"
358 " a power of two", align
);
361 } else if (!nasm_stricmp(q
, "alloc")) {
362 flags_and
|= SHF_ALLOC
;
363 flags_or
|= SHF_ALLOC
;
364 } else if (!nasm_stricmp(q
, "noalloc")) {
365 flags_and
|= SHF_ALLOC
;
366 flags_or
&= ~SHF_ALLOC
;
367 } else if (!nasm_stricmp(q
, "exec")) {
368 flags_and
|= SHF_EXECINSTR
;
369 flags_or
|= SHF_EXECINSTR
;
370 } else if (!nasm_stricmp(q
, "noexec")) {
371 flags_and
|= SHF_EXECINSTR
;
372 flags_or
&= ~SHF_EXECINSTR
;
373 } else if (!nasm_stricmp(q
, "write")) {
374 flags_and
|= SHF_WRITE
;
375 flags_or
|= SHF_WRITE
;
376 } else if (!nasm_stricmp(q
, "nowrite")) {
377 flags_and
|= SHF_WRITE
;
378 flags_or
&= ~SHF_WRITE
;
379 } else if (!nasm_stricmp(q
, "progbits")) {
381 } else if (!nasm_stricmp(q
, "nobits")) {
386 if (!strcmp(name
, ".comment") ||
387 !strcmp(name
, ".shstrtab") ||
388 !strcmp(name
, ".symtab") || !strcmp(name
, ".strtab")) {
389 error(ERR_NONFATAL
, "attempt to redefine reserved section"
394 for (i
= 0; i
< nsects
; i
++)
395 if (!strcmp(name
, sects
[i
]->name
))
398 if (!strcmp(name
, ".text"))
399 i
= elf_make_section(name
, SHT_PROGBITS
,
400 SHF_ALLOC
| SHF_EXECINSTR
, 16);
401 else if (!strcmp(name
, ".rodata"))
402 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 4);
403 else if (!strcmp(name
, ".data"))
404 i
= elf_make_section(name
, SHT_PROGBITS
,
405 SHF_ALLOC
| SHF_WRITE
, 4);
406 else if (!strcmp(name
, ".bss"))
407 i
= elf_make_section(name
, SHT_NOBITS
,
408 SHF_ALLOC
| SHF_WRITE
, 4);
410 i
= elf_make_section(name
, SHT_PROGBITS
, SHF_ALLOC
, 1);
412 sects
[i
]->type
= type
;
414 sects
[i
]->align
= align
;
415 sects
[i
]->flags
&= ~flags_and
;
416 sects
[i
]->flags
|= flags_or
;
417 } else if (pass
== 1) {
418 if (type
|| align
|| flags_and
)
419 error(ERR_WARNING
, "section attributes ignored on"
420 " redeclaration of section `%s'", name
);
423 return sects
[i
]->index
;
426 static void elf_deflabel(char *name
, long segment
, long offset
,
427 int is_global
, char *special
)
431 int special_used
= FALSE
;
433 #if defined(DEBUG) && DEBUG>2
435 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
436 name
, segment
, offset
, is_global
, special
);
438 if (name
[0] == '.' && name
[1] == '.' && name
[2] != '@') {
440 * This is a NASM special symbol. We never allow it into
441 * the ELF symbol table, even if it's a valid one. If it
442 * _isn't_ a valid one, we should barf immediately.
444 if (strcmp(name
, "..gotpc") && strcmp(name
, "..gotoff") &&
445 strcmp(name
, "..got") && strcmp(name
, "..plt") &&
446 strcmp(name
, "..sym"))
447 error(ERR_NONFATAL
, "unrecognised special symbol `%s'", name
);
451 if (is_global
== 3) {
454 * Fix up a forward-reference symbol size from the first
457 for (s
= &fwds
; *s
; s
= &(*s
)->nextfwd
)
458 if (!strcmp((*s
)->name
, name
)) {
459 struct tokenval tokval
;
463 while (*p
&& !isspace(*p
))
465 while (*p
&& isspace(*p
))
469 tokval
.t_type
= TOKEN_INVALID
;
470 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, error
, NULL
);
473 error(ERR_NONFATAL
, "cannot use relocatable"
474 " expression as symbol size");
476 (*s
)->size
= reloc_value(e
);
480 * Remove it from the list of unresolved sizes.
482 nasm_free((*s
)->name
);
486 return; /* it wasn't an important one */
489 saa_wbytes(strs
, name
, (long)(1 + strlen(name
)));
490 strslen
+= 1 + strlen(name
);
492 sym
= saa_wstruct(syms
);
495 sym
->type
= is_global
? SYM_GLOBAL
: 0;
497 if (segment
== NO_SEG
)
498 sym
->section
= SHN_ABS
;
501 sym
->section
= SHN_UNDEF
;
502 if (nsects
== 0 && segment
== def_seg
) {
504 if (segment
!= elf_section_names(".text", 2, &tempint
))
506 "strange segment conditions in ELF driver");
507 sym
->section
= nsects
;
509 for (i
= 0; i
< nsects
; i
++)
510 if (segment
== sects
[i
]->index
) {
511 sym
->section
= i
+ 1;
517 if (is_global
== 2) {
520 sym
->section
= SHN_COMMON
;
522 * We have a common variable. Check the special text to see
523 * if it's a valid number and power of two; if so, store it
524 * as the alignment for the common variable.
528 sym
->value
= readnum(special
, &err
);
530 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
531 " valid number", special
);
532 else if ((sym
->value
| (sym
->value
- 1)) != 2 * sym
->value
- 1)
533 error(ERR_NONFATAL
, "alignment constraint `%s' is not a"
534 " power of two", special
);
538 sym
->value
= (sym
->section
== SHN_UNDEF
? 0 : offset
);
540 if (sym
->type
== SYM_GLOBAL
) {
542 * There's a problem here that needs fixing.
543 * If sym->section == SHN_ABS, then the first line of the
544 * else section causes a core dump, because its a reference
545 * beyond the end of the section array.
546 * This behaviour is exhibited by this code:
550 * I'm not sure how to procede, because I haven't got the
551 * first clue about how ELF works, so I don't know what to
552 * do with it. Furthermore, I'm not sure what the rest of this
553 * section of code does. Help?
555 * For now, I'll see if doing absolutely nothing with it will
558 if (sym
->section
== SHN_UNDEF
|| sym
->section
== SHN_COMMON
) {
559 bsym
= raa_write(bsym
, segment
, nglobs
);
560 } else if (sym
->section
!= SHN_ABS
) {
562 * This is a global symbol; so we must add it to the linked
563 * list of global symbols in its section. We'll push it on
564 * the beginning of the list, because it doesn't matter
565 * much which end we put it on and it's easier like this.
567 * In addition, we check the special text for symbol
568 * type and size information.
570 sym
->next
= sects
[sym
->section
- 1]->gsyms
;
571 sects
[sym
->section
- 1]->gsyms
= sym
;
574 int n
= strcspn(special
, " ");
576 if (!nasm_strnicmp(special
, "function", n
))
577 sym
->type
|= SYM_FUNCTION
;
578 else if (!nasm_strnicmp(special
, "data", n
) ||
579 !nasm_strnicmp(special
, "object", n
))
580 sym
->type
|= SYM_DATA
;
582 error(ERR_NONFATAL
, "unrecognised symbol type `%.*s'",
585 struct tokenval tokval
;
588 char *saveme
= stdscan_bufptr
; /* bugfix? fbk 8/10/00 */
590 while (special
[n
] && isspace(special
[n
]))
593 * We have a size expression; attempt to
597 stdscan_bufptr
= special
+ n
;
598 tokval
.t_type
= TOKEN_INVALID
;
599 e
= evaluate(stdscan
, NULL
, &tokval
, &fwd
, 0, error
,
604 sym
->name
= nasm_strdup(name
);
607 error(ERR_NONFATAL
, "cannot use relocatable"
608 " expression as symbol size");
610 sym
->size
= reloc_value(e
);
612 stdscan_bufptr
= saveme
; /* bugfix? fbk 8/10/00 */
617 sym
->globnum
= nglobs
;
622 if (special
&& !special_used
)
623 error(ERR_NONFATAL
, "no special symbol features supported here");
626 static void elf_add_reloc(struct Section
*sect
, long segment
, int type
)
630 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
631 sect
->tail
= &r
->next
;
634 r
->address
= sect
->len
;
635 if (segment
== NO_SEG
)
640 for (i
= 0; i
< nsects
; i
++)
641 if (segment
== sects
[i
]->index
)
644 r
->symbol
= GLOBAL_TEMP_BASE
+ raa_read(bsym
, segment
);
652 * This routine deals with ..got and ..sym relocations: the more
653 * complicated kinds. In shared-library writing, some relocations
654 * with respect to global symbols must refer to the precise symbol
655 * rather than referring to an offset from the base of the section
656 * _containing_ the symbol. Such relocations call to this routine,
657 * which searches the symbol list for the symbol in question.
659 * R_386_GOT32 references require the _exact_ symbol address to be
660 * used; R_386_32 references can be at an offset from the symbol.
661 * The boolean argument `exact' tells us this.
663 * Return value is the adjusted value of `addr', having become an
664 * offset from the symbol rather than the section. Should always be
665 * zero when returning from an exact call.
667 * Limitation: if you define two symbols at the same place,
668 * confusion will occur.
670 * Inefficiency: we search, currently, using a linked list which
671 * isn't even necessarily sorted.
673 static long elf_add_gsym_reloc(struct Section
*sect
,
674 long segment
, long offset
,
679 struct Symbol
*sym
, *sm
;
683 * First look up the segment/offset pair and find a global
684 * symbol corresponding to it. If it's not one of our segments,
685 * then it must be an external symbol, in which case we're fine
686 * doing a normal elf_add_reloc after first sanity-checking
687 * that the offset from the symbol is zero.
690 for (i
= 0; i
< nsects
; i
++)
691 if (segment
== sects
[i
]->index
) {
696 if (exact
&& offset
!= 0)
697 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
698 " for this reference");
700 elf_add_reloc(sect
, segment
, type
);
706 * Find a symbol pointing _exactly_ at this one.
708 for (sym
= s
->gsyms
; sym
; sym
= sym
->next
)
709 if (sym
->value
== offset
)
713 * Find the nearest symbol below this one.
716 for (sm
= s
->gsyms
; sm
; sm
= sm
->next
)
717 if (sm
->value
<= offset
&& (!sym
|| sm
->value
> sym
->value
))
721 error(ERR_NONFATAL
, "unable to find a suitable global symbol"
722 " for this reference");
726 r
= *sect
->tail
= nasm_malloc(sizeof(struct Reloc
));
727 sect
->tail
= &r
->next
;
730 r
->address
= sect
->len
;
731 r
->symbol
= GLOBAL_TEMP_BASE
+ sym
->globnum
;
736 return offset
- sym
->value
;
739 static void elf_out(long segto
, const void *data
, unsigned long type
,
740 long segment
, long wrt
)
743 long realbytes
= type
& OUT_SIZMASK
;
745 unsigned char mydata
[4], *p
;
747 static struct symlininfo sinfo
;
752 * handle absolute-assembly (structure definitions)
754 if (segto
== NO_SEG
) {
755 if (type
!= OUT_RESERVE
)
756 error(ERR_NONFATAL
, "attempt to assemble code in [ABSOLUTE]"
762 for (i
= 0; i
< nsects
; i
++)
763 if (segto
== sects
[i
]->index
) {
768 int tempint
; /* ignored */
769 if (segto
!= elf_section_names(".text", 2, &tempint
))
770 error(ERR_PANIC
, "strange segment conditions in ELF driver");
772 s
= sects
[nsects
- 1];
777 /* again some stabs debugging stuff */
778 if (of_elf
.current_dfmt
) {
779 sinfo
.offset
= s
->len
;
781 sinfo
.name
= s
->name
;
782 of_elf
.current_dfmt
->debug_output(TY_STABSSYMLIN
, &sinfo
);
784 /* end of debugging stuff */
786 if (s
->type
== SHT_NOBITS
&& type
!= OUT_RESERVE
) {
787 error(ERR_WARNING
, "attempt to initialise memory in"
788 " BSS section `%s': ignored", s
->name
);
789 if (type
== OUT_REL2ADR
)
791 else if (type
== OUT_REL4ADR
)
797 if (type
== OUT_RESERVE
) {
798 if (s
->type
== SHT_PROGBITS
) {
799 error(ERR_WARNING
, "uninitialised space declared in"
800 " non-BSS section `%s': zeroing", s
->name
);
801 elf_sect_write(s
, NULL
, realbytes
);
804 } else if (type
== OUT_RAWDATA
) {
805 if (segment
!= NO_SEG
)
806 error(ERR_PANIC
, "OUT_RAWDATA with other than NO_SEG");
807 elf_sect_write(s
, data
, realbytes
);
808 } else if (type
== OUT_ADDRESS
) {
810 addr
= *(long *)data
;
811 if (segment
!= NO_SEG
) {
813 error(ERR_NONFATAL
, "ELF format does not support"
814 " segment base references");
817 if (realbytes
== 2) {
819 elf_add_reloc(s
, segment
, R_386_16
);
821 elf_add_reloc(s
, segment
, R_386_32
);
823 } else if (wrt
== elf_gotpc_sect
+ 1) {
825 * The user will supply GOT relative to $$. ELF
826 * will let us have GOT relative to $. So we
827 * need to fix up the data item by $-$$.
830 elf_add_reloc(s
, segment
, R_386_GOTPC
);
831 } else if (wrt
== elf_gotoff_sect
+ 1) {
832 elf_add_reloc(s
, segment
, R_386_GOTOFF
);
833 } else if (wrt
== elf_got_sect
+ 1) {
834 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
836 } else if (wrt
== elf_sym_sect
+ 1) {
837 if (realbytes
== 2) {
839 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
842 addr
= elf_add_gsym_reloc(s
, segment
, addr
,
845 } else if (wrt
== elf_plt_sect
+ 1) {
846 error(ERR_NONFATAL
, "ELF format cannot produce non-PC-"
847 "relative PLT references");
849 error(ERR_NONFATAL
, "ELF format does not support this"
851 wrt
= NO_SEG
; /* we can at least _try_ to continue */
857 error(ERR_WARNING
| ERR_WARN_GNUELF
,
858 "16-bit relocations in ELF is a GNU extension");
861 if (realbytes
!= 4 && segment
!= NO_SEG
) {
863 "Unsupported non-32-bit ELF relocation");
867 elf_sect_write(s
, mydata
, realbytes
);
868 } else if (type
== OUT_REL2ADR
) {
869 if (segment
== segto
)
870 error(ERR_PANIC
, "intra-segment OUT_REL2ADR");
871 if (segment
!= NO_SEG
&& segment
% 2) {
872 error(ERR_NONFATAL
, "ELF format does not support"
873 " segment base references");
876 error(ERR_WARNING
| ERR_WARN_GNUELF
,
877 "16-bit relocations in ELF is a GNU extension");
878 elf_add_reloc(s
, segment
, R_386_PC16
);
881 "Unsupported non-32-bit ELF relocation");
885 WRITESHORT(p
, *(long *)data
- realbytes
);
886 elf_sect_write(s
, mydata
, 2L);
887 } else if (type
== OUT_REL4ADR
) {
888 if (segment
== segto
)
889 error(ERR_PANIC
, "intra-segment OUT_REL4ADR");
890 if (segment
!= NO_SEG
&& segment
% 2) {
891 error(ERR_NONFATAL
, "ELF format does not support"
892 " segment base references");
895 elf_add_reloc(s
, segment
, R_386_PC32
);
896 } else if (wrt
== elf_plt_sect
+ 1) {
897 elf_add_reloc(s
, segment
, R_386_PLT32
);
898 } else if (wrt
== elf_gotpc_sect
+ 1 ||
899 wrt
== elf_gotoff_sect
+ 1 ||
900 wrt
== elf_got_sect
+ 1) {
901 error(ERR_NONFATAL
, "ELF format cannot produce PC-"
902 "relative GOT references");
904 error(ERR_NONFATAL
, "ELF format does not support this"
906 wrt
= NO_SEG
; /* we can at least _try_ to continue */
910 WRITELONG(p
, *(long *)data
- realbytes
);
911 elf_sect_write(s
, mydata
, 4L);
915 static void elf_write(void)
917 int nsections
, align
;
925 long symtablen
, symtablocal
;
928 * Work out how many sections we will have. We have SHN_UNDEF,
929 * then the flexible user sections, then the four fixed
930 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
931 * then optionally relocation sections for the user sections.
933 if (of_elf
.current_dfmt
== &df_stabs
)
936 nsections
= 5; /* SHN_UNDEF and the fixed ones */
938 add_sectname("", ".comment");
939 add_sectname("", ".shstrtab");
940 add_sectname("", ".symtab");
941 add_sectname("", ".strtab");
942 for (i
= 0; i
< nsects
; i
++) {
943 nsections
++; /* for the section itself */
944 if (sects
[i
]->head
) {
945 nsections
++; /* for its relocations */
946 add_sectname(".rel", sects
[i
]->name
);
950 if (of_elf
.current_dfmt
== &df_stabs
) {
951 /* in case the debug information is wanted, just add these three sections... */
952 add_sectname("", ".stab");
953 add_sectname("", ".stabstr");
954 add_sectname(".rel", ".stab");
962 2 + sprintf(comment
+ 1, "The Netwide Assembler %s", NASM_VER
);
965 * Output the ELF header.
967 fwrite("\177ELF\1\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp
);
968 fwriteshort(1, elffp
); /* ET_REL relocatable file */
969 fwriteshort(3, elffp
); /* EM_386 processor ID */
970 fwritelong(1L, elffp
); /* EV_CURRENT file format version */
971 fwritelong(0L, elffp
); /* no entry point */
972 fwritelong(0L, elffp
); /* no program header table */
973 fwritelong(0x40L
, elffp
); /* section headers straight after
974 * ELF header plus alignment */
975 fwritelong(0L, elffp
); /* 386 defines no special flags */
976 fwriteshort(0x34, elffp
); /* size of ELF header */
977 fwriteshort(0, elffp
); /* no program header table, again */
978 fwriteshort(0, elffp
); /* still no program header table */
979 fwriteshort(0x28, elffp
); /* size of section header */
980 fwriteshort(nsections
, elffp
); /* number of sections */
981 fwriteshort(nsects
+ 2, elffp
); /* string table section index for
982 * section header table */
983 fwritelong(0L, elffp
); /* align to 0x40 bytes */
984 fwritelong(0L, elffp
);
985 fwritelong(0L, elffp
);
988 * Build the symbol table and relocation tables.
990 symtab
= elf_build_symtab(&symtablen
, &symtablocal
);
991 for (i
= 0; i
< nsects
; i
++)
993 sects
[i
]->rel
= elf_build_reltab(§s
[i
]->rellen
,
997 * Now output the section header table.
1000 elf_foffs
= 0x40 + 0x28 * nsections
;
1001 align
= ((elf_foffs
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
) - elf_foffs
;
1004 elf_sects
= nasm_malloc(sizeof(*elf_sects
) * (2 * nsects
+ 10));
1006 elf_section_header(0, 0, 0, NULL
, FALSE
, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1007 scount
= 1; /* needed for the stabs debugging to track the symtable section */
1009 for (i
= 0; i
< nsects
; i
++) {
1010 elf_section_header(p
- shstrtab
, sects
[i
]->type
, sects
[i
]->flags
,
1011 (sects
[i
]->type
== SHT_PROGBITS
?
1012 sects
[i
]->data
: NULL
), TRUE
,
1013 sects
[i
]->len
, 0, 0, sects
[i
]->align
, 0);
1015 scount
++; /* dito */
1017 elf_section_header(p
- shstrtab
, 1, 0, comment
, FALSE
, (long)commlen
, 0, 0, 1, 0); /* .comment */
1018 scount
++; /* dito */
1020 elf_section_header(p
- shstrtab
, 3, 0, shstrtab
, FALSE
, (long)shstrtablen
, 0, 0, 1, 0); /* .shstrtab */
1021 scount
++; /* dito */
1023 elf_section_header(p
- shstrtab
, 2, 0, symtab
, TRUE
, symtablen
, nsects
+ 4, symtablocal
, 4, 16); /* .symtab */
1024 symtabsection
= scount
; /* now we got the symtab section index in the ELF file */
1026 elf_section_header(p
- shstrtab
, 3, 0, strs
, TRUE
, strslen
, 0, 0, 1, 0); /* .strtab */
1027 for (i
= 0; i
< nsects
; i
++)
1028 if (sects
[i
]->head
) {
1030 elf_section_header(p
- shstrtab
, 9, 0, sects
[i
]->rel
, TRUE
,
1031 sects
[i
]->rellen
, nsects
+ 3, i
+ 1, 4, 8);
1033 if (of_elf
.current_dfmt
== &df_stabs
) {
1034 /* for debugging information, create the last three sections
1035 which are the .stab , .stabstr and .rel.stab sections respectively */
1037 /* this function call creates the stab sections in memory */
1040 if ((stabbuf
) && (stabstrbuf
) && (stabrelbuf
)) {
1042 elf_section_header(p
- shstrtab
, 1, 0, stabbuf
, 0, stablen
,
1043 nsections
- 2, 0, 4, 12);
1046 elf_section_header(p
- shstrtab
, 3, 0, stabstrbuf
, 0,
1047 stabstrlen
, 0, 0, 4, 0);
1050 /* link -> symtable info -> section to refer to */
1051 elf_section_header(p
- shstrtab
, 9, 0, stabrelbuf
, 0,
1052 stabrellen
, symtabsection
, nsections
- 3, 4,
1056 fwrite(align_str
, align
, 1, elffp
);
1059 * Now output the sections.
1061 elf_write_sections();
1063 nasm_free(elf_sects
);
1067 static struct SAA
*elf_build_symtab(long *len
, long *local
)
1069 struct SAA
*s
= saa_init(1L);
1071 unsigned char entry
[16], *p
;
1077 * First, an all-zeros entry, required by the ELF spec.
1079 saa_wbytes(s
, NULL
, 16L); /* null symbol table entry */
1084 * Next, an entry for the file name.
1087 WRITELONG(p
, 1); /* we know it's 1st thing in strtab */
1088 WRITELONG(p
, 0); /* no value */
1089 WRITELONG(p
, 0); /* no size either */
1090 WRITESHORT(p
, 4); /* type FILE */
1091 WRITESHORT(p
, SHN_ABS
);
1092 saa_wbytes(s
, entry
, 16L);
1097 * Now some standard symbols defining the segments, for relocation
1100 for (i
= 1; i
<= nsects
+ 1; i
++) {
1102 WRITELONG(p
, 0); /* no symbol name */
1103 WRITELONG(p
, 0); /* offset zero */
1104 WRITELONG(p
, 0); /* size zero */
1105 WRITESHORT(p
, 3); /* local section-type thing */
1106 WRITESHORT(p
, (i
== 1 ? SHN_ABS
: i
- 1)); /* the section id */
1107 saa_wbytes(s
, entry
, 16L);
1113 * Now the other local symbols.
1116 while ((sym
= saa_rstruct(syms
))) {
1117 if (sym
->type
& SYM_GLOBAL
)
1120 WRITELONG(p
, sym
->strpos
);
1121 WRITELONG(p
, sym
->value
);
1122 WRITELONG(p
, sym
->size
);
1123 WRITESHORT(p
, sym
->type
); /* local non-typed thing */
1124 WRITESHORT(p
, sym
->section
);
1125 saa_wbytes(s
, entry
, 16L);
1131 * Now the global symbols.
1134 while ((sym
= saa_rstruct(syms
))) {
1135 if (!(sym
->type
& SYM_GLOBAL
))
1138 WRITELONG(p
, sym
->strpos
);
1139 WRITELONG(p
, sym
->value
);
1140 WRITELONG(p
, sym
->size
);
1141 WRITESHORT(p
, sym
->type
); /* global non-typed thing */
1142 WRITESHORT(p
, sym
->section
);
1143 saa_wbytes(s
, entry
, 16L);
1150 static struct SAA
*elf_build_reltab(long *len
, struct Reloc
*r
)
1153 unsigned char *p
, entry
[8];
1162 long sym
= r
->symbol
;
1164 if (sym
>= GLOBAL_TEMP_BASE
)
1165 sym
+= -GLOBAL_TEMP_BASE
+ (nsects
+ 3) + nlocals
;
1168 WRITELONG(p
, r
->address
);
1169 WRITELONG(p
, (sym
<< 8) + r
->type
);
1170 saa_wbytes(s
, entry
, 8L);
1179 static void elf_section_header(int name
, int type
, int flags
,
1180 void *data
, int is_saa
, long datalen
,
1181 int link
, int info
, int align
, int eltsize
)
1183 elf_sects
[elf_nsect
].data
= data
;
1184 elf_sects
[elf_nsect
].len
= datalen
;
1185 elf_sects
[elf_nsect
].is_saa
= is_saa
;
1188 fwritelong((long)name
, elffp
);
1189 fwritelong((long)type
, elffp
);
1190 fwritelong((long)flags
, elffp
);
1191 fwritelong(0L, elffp
); /* no address, ever, in object files */
1192 fwritelong(type
== 0 ? 0L : elf_foffs
, elffp
);
1193 fwritelong(datalen
, elffp
);
1195 elf_foffs
+= (datalen
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1196 fwritelong((long)link
, elffp
);
1197 fwritelong((long)info
, elffp
);
1198 fwritelong((long)align
, elffp
);
1199 fwritelong((long)eltsize
, elffp
);
1202 static void elf_write_sections(void)
1205 for (i
= 0; i
< elf_nsect
; i
++)
1206 if (elf_sects
[i
].data
) {
1207 long len
= elf_sects
[i
].len
;
1208 long reallen
= (len
+ SEG_ALIGN_1
) & ~SEG_ALIGN_1
;
1209 long align
= reallen
- len
;
1210 if (elf_sects
[i
].is_saa
)
1211 saa_fpwrite(elf_sects
[i
].data
, elffp
);
1213 fwrite(elf_sects
[i
].data
, len
, 1, elffp
);
1214 fwrite(align_str
, align
, 1, elffp
);
1218 static void elf_sect_write(struct Section
*sect
,
1219 const unsigned char *data
, unsigned long len
)
1221 saa_wbytes(sect
->data
, data
, len
);
1225 static long elf_segbase(long segment
)
1230 static int elf_directive(char *directive
, char *value
, int pass
)
1235 static void elf_filename(char *inname
, char *outname
, efunc error
)
1237 strcpy(elf_module
, inname
);
1238 standard_extension(inname
, outname
, ".o", error
);
1241 static const char *elf_stdmac
[] = {
1242 "%define __SECT__ [section .text]",
1243 "%macro __NASM_CDecl__ 1",
1248 static int elf_set_info(enum geninfo type
, char **val
)
1253 static struct dfmt df_stabs
= {
1254 "ELF32 (i386) stabs debug format for Linux",
1265 struct dfmt
*elf_debugs_arr
[2] = { &df_stabs
, NULL
};
1267 struct ofmt of_elf
= {
1268 "ELF32 (i386) object files (e.g. Linux)",
1285 /* again, the stabs debugging stuff (code) */
1287 void stabs_init(struct ofmt
*of
, void *id
, FILE * fp
, efunc error
)
1291 void stabs_linenum(const char *filename
, long linenumber
, long segto
)
1293 if (!stabs_filename
) {
1294 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1295 strcpy(stabs_filename
, filename
);
1297 if (strcmp(stabs_filename
, filename
)) {
1298 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1299 in fact, this leak comes in quite handy to maintain a list of files
1300 encountered so far in the symbol lines... */
1302 /* why not nasm_free(stabs_filename); we're done with the old one */
1304 stabs_filename
= (char *)nasm_malloc(strlen(filename
) + 1);
1305 strcpy(stabs_filename
, filename
);
1309 currentline
= linenumber
;
1312 void stabs_deflabel(char *name
, long segment
, long offset
, int is_global
,
1317 void stabs_directive(const char *directive
, const char *params
)
1321 void stabs_typevalue(long type
)
1325 void stabs_output(int type
, void *param
)
1327 struct symlininfo
*s
;
1328 struct linelist
*el
;
1329 if (type
== TY_STABSSYMLIN
) {
1330 if (stabs_immcall
) {
1331 s
= (struct symlininfo
*)param
;
1332 if (strcmp(s
->name
, ".text"))
1333 return; /* we are only interested in the text stuff */
1335 el
= (struct linelist
*)nasm_malloc(sizeof(struct linelist
));
1336 el
->info
.offset
= s
->offset
;
1337 el
->info
.section
= s
->section
;
1338 el
->info
.name
= s
->name
;
1339 el
->line
= currentline
;
1340 el
->filename
= stabs_filename
;
1343 stabslines
->last
->next
= el
;
1344 stabslines
->last
= el
;
1347 stabslines
->last
= el
;
1354 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1356 WRITELONG(p,n_strx); \
1357 WRITECHAR(p,n_type); \
1358 WRITECHAR(p,n_other); \
1359 WRITESHORT(p,n_desc); \
1360 WRITELONG(p,n_value); \
1363 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1365 void stabs_generate(void)
1367 int i
, numfiles
, strsize
, numstabs
= 0, currfile
, mainfileindex
;
1368 unsigned char *sbuf
, *ssbuf
, *rbuf
, *sptr
, *rptr
;
1372 struct linelist
*ptr
;
1376 allfiles
= (char **)nasm_malloc(numlinestabs
* sizeof(char *));
1377 for (i
= 0; i
< numlinestabs
; i
++)
1381 if (numfiles
== 0) {
1382 allfiles
[0] = ptr
->filename
;
1385 for (i
= 0; i
< numfiles
; i
++) {
1386 if (!strcmp(allfiles
[i
], ptr
->filename
))
1389 if (i
>= numfiles
) {
1390 allfiles
[i
] = ptr
->filename
;
1397 fileidx
= (int *)nasm_malloc(numfiles
* sizeof(int));
1398 for (i
= 0; i
< numfiles
; i
++) {
1399 fileidx
[i
] = strsize
;
1400 strsize
+= strlen(allfiles
[i
]) + 1;
1403 for (i
= 0; i
< numfiles
; i
++) {
1404 if (!strcmp(allfiles
[i
], elf_module
)) {
1410 /* worst case size of the stab buffer would be:
1411 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1414 (unsigned char *)nasm_malloc((numlinestabs
* 2 + 3) *
1415 sizeof(struct stabentry
));
1417 ssbuf
= (unsigned char *)nasm_malloc(strsize
);
1419 rbuf
= (unsigned char *)nasm_malloc(numlinestabs
* 8 * (2 + 3));
1422 for (i
= 0; i
< numfiles
; i
++) {
1423 strcpy((char *)ssbuf
+ fileidx
[i
], allfiles
[i
]);
1427 stabstrlen
= strsize
; /* set global variable for length of stab strings */
1430 /* this is the first stab, its strx points to the filename of the
1431 the source-file, the n_desc field should be set to the number
1434 WRITE_STAB(sptr
, fileidx
[0], 0, 0, 0, strlen(allfiles
[0] + 12));
1440 /* this is the stab for the main source file */
1441 WRITE_STAB(sptr
, fileidx
[mainfileindex
], N_SO
, 0, 0, 0);
1443 /* relocation stuff */
1444 /* IS THIS SANE? WHAT DOES SECTION+3 MEAN HERE? */
1445 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1446 WRITELONG(rptr
, ((ptr
->info
.section
+ 3) << 8) | R_386_32
);
1449 currfile
= mainfileindex
;
1453 if (strcmp(allfiles
[currfile
], ptr
->filename
)) {
1454 /* oops file has changed... */
1455 for (i
= 0; i
< numfiles
; i
++)
1456 if (!strcmp(allfiles
[i
], ptr
->filename
))
1459 WRITE_STAB(sptr
, fileidx
[currfile
], N_SOL
, 0, 0,
1463 /* relocation stuff */
1464 /* IS THIS SANE? WHAT DOES SECTION+3 MEAN HERE? */
1465 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1466 WRITELONG(rptr
, ((ptr
->info
.section
+ 3) << 8) | R_386_32
);
1469 WRITE_STAB(sptr
, 0, N_SLINE
, 0, ptr
->line
, ptr
->info
.offset
);
1472 /* relocation stuff */
1473 /* IS THIS SANE? WHAT DOES SECTION+3 MEAN HERE? */
1474 WRITELONG(rptr
, (sptr
- sbuf
) - 4);
1475 WRITELONG(rptr
, ((ptr
->info
.section
+ 3) << 8) | R_386_32
);
1481 ((struct stabentry
*)sbuf
)->n_desc
= numstabs
;
1483 nasm_free(allfiles
);
1486 stablen
= (sptr
- sbuf
);
1487 stabrellen
= (rptr
- rbuf
);
1493 void stabs_cleanup()
1495 struct linelist
*ptr
, *del
;
1507 nasm_free(stabrelbuf
);
1509 nasm_free(stabstrbuf
);