specfile: correctly handle compressed manpages
[nasm/avx512.git] / output / outelf32.c
blob33fe1b98f28e33bfcb498faffee6cb1663bda94a
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.
8 */
10 #include "compiler.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <inttypes.h>
18 #include "nasm.h"
19 #include "nasmlib.h"
20 #include "stdscan.h"
21 #include "outform.h"
23 #ifdef OF_ELF32
26 * Relocation types.
28 enum reloc_type {
29 R_386_32 = 1, /* ordinary absolute relocation */
30 R_386_PC32 = 2, /* PC-relative relocation */
31 R_386_GOT32 = 3, /* an offset into GOT */
32 R_386_PLT32 = 4, /* a PC-relative offset into PLT */
33 R_386_COPY = 5, /* ??? */
34 R_386_GLOB_DAT = 6, /* ??? */
35 R_386_JUMP_SLOT = 7, /* ??? */
36 R_386_RELATIVE = 8, /* ??? */
37 R_386_GOTOFF = 9, /* an offset from GOT base */
38 R_386_GOTPC = 10, /* a PC-relative offset _to_ GOT */
39 /* These are GNU extensions, but useful */
40 R_386_16 = 20, /* A 16-bit absolute relocation */
41 R_386_PC16 = 21, /* A 16-bit PC-relative relocation */
42 R_386_8 = 22, /* An 8-bit absolute relocation */
43 R_386_PC8 = 23 /* An 8-bit PC-relative relocation */
46 struct Reloc {
47 struct Reloc *next;
48 int32_t address; /* relative to _start_ of section */
49 int32_t symbol; /* symbol index */
50 int type; /* type of relocation */
53 struct Symbol {
54 int32_t strpos; /* string table position of name */
55 int32_t section; /* section ID of the symbol */
56 int type; /* symbol type */
57 int other; /* symbol visibility */
58 int32_t value; /* address, or COMMON variable align */
59 int32_t size; /* size of symbol */
60 int32_t globnum; /* symbol table offset if global */
61 struct Symbol *next; /* list of globals in each section */
62 struct Symbol *nextfwd; /* list of unresolved-size symbols */
63 char *name; /* used temporarily if in above list */
66 #define SHT_PROGBITS 1
67 #define SHT_NOBITS 8
69 #define SHF_WRITE 1
70 #define SHF_ALLOC 2
71 #define SHF_EXECINSTR 4
73 struct Section {
74 struct SAA *data;
75 uint32_t len, size, nrelocs;
76 int32_t index;
77 int type; /* SHT_PROGBITS or SHT_NOBITS */
78 int align; /* alignment: power of two */
79 uint32_t flags; /* section flags */
80 char *name;
81 struct SAA *rel;
82 int32_t rellen;
83 struct Reloc *head, **tail;
84 struct Symbol *gsyms; /* global symbols in section */
87 #define SECT_DELTA 32
88 static struct Section **sects;
89 static int nsects, sectlen;
91 #define SHSTR_DELTA 256
92 static char *shstrtab;
93 static int shstrtablen, shstrtabsize;
95 static struct SAA *syms;
96 static uint32_t nlocals, nglobs;
98 static int32_t def_seg;
100 static struct RAA *bsym;
102 static struct SAA *strs;
103 static uint32_t strslen;
105 static FILE *elffp;
106 static efunc error;
107 static evalfunc evaluate;
109 static struct Symbol *fwds;
111 static char elf_module[FILENAME_MAX];
113 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
114 static uint8_t elf_abiver = 0; /* Current ABI version */
116 extern struct ofmt of_elf32;
117 extern struct ofmt of_elf;
119 #define SHN_ABS 0xFFF1
120 #define SHN_COMMON 0xFFF2
121 #define SHN_UNDEF 0
123 #define SYM_GLOBAL 0x10
125 #define STT_NOTYPE 0 /* Symbol type is unspecified */
126 #define STT_OBJECT 1 /* Symbol is a data object */
127 #define STT_FUNC 2 /* Symbol is a code object */
128 #define STT_SECTION 3 /* Symbol associated with a section */
129 #define STT_FILE 4 /* Symbol's name is file name */
130 #define STT_COMMON 5 /* Symbol is a common data object */
131 #define STT_TLS 6 /* Symbol is thread-local data object*/
132 #define STT_NUM 7 /* Number of defined types. */
134 #define STV_DEFAULT 0
135 #define STV_INTERNAL 1
136 #define STV_HIDDEN 2
137 #define STV_PROTECTED 3
139 #define GLOBAL_TEMP_BASE 16 /* bigger than any constant sym id */
141 #define SEG_ALIGN 16 /* alignment of sections in file */
142 #define SEG_ALIGN_1 (SEG_ALIGN-1)
144 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
146 #define ELF_MAX_SECTIONS 16 /* really 10, but let's play safe */
147 static struct ELF_SECTDATA {
148 void *data;
149 int32_t len;
150 bool is_saa;
151 } *elf_sects;
152 static int elf_nsect;
153 static int32_t elf_foffs;
155 static void elf_write(void);
156 static void elf_sect_write(struct Section *, const uint8_t *,
157 uint32_t);
158 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
159 int, int);
160 static void elf_write_sections(void);
161 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
162 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
163 static void add_sectname(char *, char *);
165 /* this stuff is needed for the stabs debugging format */
166 #define N_SO 0x64 /* ID for main source file */
167 #define N_SOL 0x84 /* ID for sub-source file */
168 #define N_BINCL 0x82
169 #define N_EINCL 0xA2
170 #define N_SLINE 0x44
171 #define TY_STABSSYMLIN 0x40 /* ouch */
173 struct stabentry {
174 uint32_t n_strx;
175 uint8_t n_type;
176 uint8_t n_other;
177 uint16_t n_desc;
178 uint32_t n_value;
181 struct erel {
182 int offset, info;
185 struct symlininfo {
186 int offset;
187 int section; /* section index */
188 char *name; /* shallow-copied pointer of section name */
191 struct linelist {
192 struct symlininfo info;
193 int line;
194 char *filename;
195 struct linelist *next;
196 struct linelist *last;
199 static struct linelist *stabslines = 0;
200 static int stabs_immcall = 0;
201 static int currentline = 0;
202 static int numlinestabs = 0;
203 static char *stabs_filename = 0;
204 static int symtabsection;
205 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
206 static int stablen, stabstrlen, stabrellen;
208 static struct dfmt df_stabs;
209 static struct Symbol *lastsym;
211 void stabs32_init(struct ofmt *, void *, FILE *, efunc);
212 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
213 void stabs32_deflabel(char *, int32_t, int64_t, int, char *);
214 void stabs32_directive(const char *, const char *);
215 void stabs32_typevalue(int32_t);
216 void stabs32_output(int, void *);
217 void stabs32_generate(void);
218 void stabs32_cleanup(void);
220 /* end of stabs debugging stuff */
223 * Special section numbers which are used to define ELF special
224 * symbols, which can be used with WRT to provide PIC relocation
225 * types.
227 static int32_t elf_gotpc_sect, elf_gotoff_sect;
228 static int32_t elf_got_sect, elf_plt_sect;
229 static int32_t elf_sym_sect;
231 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
233 if (of_elf.current_dfmt != &null_debug_form)
234 of_elf32.current_dfmt = of_elf.current_dfmt;
235 elffp = fp;
236 error = errfunc;
237 evaluate = eval;
238 (void)ldef; /* placate optimisers */
239 sects = NULL;
240 nsects = sectlen = 0;
241 syms = saa_init((int32_t)sizeof(struct Symbol));
242 nlocals = nglobs = 0;
243 bsym = raa_init();
244 strs = saa_init(1L);
245 saa_wbytes(strs, "\0", 1L);
246 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
247 strslen = 2 + strlen(elf_module);
248 shstrtab = NULL;
249 shstrtablen = shstrtabsize = 0;;
250 add_sectname("", "");
252 fwds = NULL;
254 elf_gotpc_sect = seg_alloc();
255 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf32,
256 error);
257 elf_gotoff_sect = seg_alloc();
258 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf32,
259 error);
260 elf_got_sect = seg_alloc();
261 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf32,
262 error);
263 elf_plt_sect = seg_alloc();
264 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf32,
265 error);
266 elf_sym_sect = seg_alloc();
267 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf32,
268 error);
270 def_seg = seg_alloc();
273 static void elf_cleanup(int debuginfo)
275 struct Reloc *r;
276 int i;
278 (void)debuginfo;
280 elf_write();
281 fclose(elffp);
282 for (i = 0; i < nsects; i++) {
283 if (sects[i]->type != SHT_NOBITS)
284 saa_free(sects[i]->data);
285 if (sects[i]->head)
286 saa_free(sects[i]->rel);
287 while (sects[i]->head) {
288 r = sects[i]->head;
289 sects[i]->head = sects[i]->head->next;
290 nasm_free(r);
293 nasm_free(sects);
294 saa_free(syms);
295 raa_free(bsym);
296 saa_free(strs);
297 if (of_elf32.current_dfmt) {
298 of_elf32.current_dfmt->cleanup();
302 static void add_sectname(char *firsthalf, char *secondhalf)
304 int len = strlen(firsthalf) + strlen(secondhalf);
305 while (shstrtablen + len + 1 > shstrtabsize)
306 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
307 strcpy(shstrtab + shstrtablen, firsthalf);
308 strcat(shstrtab + shstrtablen, secondhalf);
309 shstrtablen += len + 1;
312 static int elf_make_section(char *name, int type, int flags, int align)
314 struct Section *s;
316 s = nasm_malloc(sizeof(*s));
318 if (type != SHT_NOBITS)
319 s->data = saa_init(1L);
320 s->head = NULL;
321 s->tail = &s->head;
322 s->len = s->size = 0;
323 s->nrelocs = 0;
324 if (!strcmp(name, ".text"))
325 s->index = def_seg;
326 else
327 s->index = seg_alloc();
328 add_sectname("", name);
329 s->name = nasm_malloc(1 + strlen(name));
330 strcpy(s->name, name);
331 s->type = type;
332 s->flags = flags;
333 s->align = align;
334 s->gsyms = NULL;
336 if (nsects >= sectlen)
337 sects =
338 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
339 sects[nsects++] = s;
341 return nsects - 1;
344 static int32_t elf_section_names(char *name, int pass, int *bits)
346 char *p;
347 unsigned flags_and, flags_or;
348 int type, align, i;
351 * Default is 32 bits.
353 if (!name) {
354 *bits = 32;
355 return def_seg;
358 p = name;
359 while (*p && !isspace(*p))
360 p++;
361 if (*p)
362 *p++ = '\0';
363 flags_and = flags_or = type = align = 0;
365 while (*p && isspace(*p))
366 p++;
367 while (*p) {
368 char *q = p;
369 while (*p && !isspace(*p))
370 p++;
371 if (*p)
372 *p++ = '\0';
373 while (*p && isspace(*p))
374 p++;
376 if (!nasm_strnicmp(q, "align=", 6)) {
377 align = atoi(q + 6);
378 if (align == 0)
379 align = 1;
380 if ((align - 1) & align) { /* means it's not a power of two */
381 error(ERR_NONFATAL, "section alignment %d is not"
382 " a power of two", align);
383 align = 1;
385 } else if (!nasm_stricmp(q, "alloc")) {
386 flags_and |= SHF_ALLOC;
387 flags_or |= SHF_ALLOC;
388 } else if (!nasm_stricmp(q, "noalloc")) {
389 flags_and |= SHF_ALLOC;
390 flags_or &= ~SHF_ALLOC;
391 } else if (!nasm_stricmp(q, "exec")) {
392 flags_and |= SHF_EXECINSTR;
393 flags_or |= SHF_EXECINSTR;
394 } else if (!nasm_stricmp(q, "noexec")) {
395 flags_and |= SHF_EXECINSTR;
396 flags_or &= ~SHF_EXECINSTR;
397 } else if (!nasm_stricmp(q, "write")) {
398 flags_and |= SHF_WRITE;
399 flags_or |= SHF_WRITE;
400 } else if (!nasm_stricmp(q, "nowrite")) {
401 flags_and |= SHF_WRITE;
402 flags_or &= ~SHF_WRITE;
403 } else if (!nasm_stricmp(q, "progbits")) {
404 type = SHT_PROGBITS;
405 } else if (!nasm_stricmp(q, "nobits")) {
406 type = SHT_NOBITS;
410 if (!strcmp(name, ".comment") ||
411 !strcmp(name, ".shstrtab") ||
412 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
413 error(ERR_NONFATAL, "attempt to redefine reserved section"
414 "name `%s'", name);
415 return NO_SEG;
418 for (i = 0; i < nsects; i++)
419 if (!strcmp(name, sects[i]->name))
420 break;
421 if (i == nsects) {
422 if (!strcmp(name, ".text"))
423 i = elf_make_section(name, SHT_PROGBITS,
424 SHF_ALLOC | SHF_EXECINSTR, 16);
425 else if (!strcmp(name, ".rodata"))
426 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
427 else if (!strcmp(name, ".data"))
428 i = elf_make_section(name, SHT_PROGBITS,
429 SHF_ALLOC | SHF_WRITE, 4);
430 else if (!strcmp(name, ".bss"))
431 i = elf_make_section(name, SHT_NOBITS,
432 SHF_ALLOC | SHF_WRITE, 4);
433 else
434 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
435 if (type)
436 sects[i]->type = type;
437 if (align)
438 sects[i]->align = align;
439 sects[i]->flags &= ~flags_and;
440 sects[i]->flags |= flags_or;
441 } else if (pass == 1) {
442 if ((type && sects[i]->type != type)
443 || (align && sects[i]->align != align)
444 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
445 error(ERR_WARNING, "section attributes ignored on"
446 " redeclaration of section `%s'", name);
449 return sects[i]->index;
452 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
453 int is_global, char *special)
455 int pos = strslen;
456 struct Symbol *sym;
457 bool special_used = false;
459 #if defined(DEBUG) && DEBUG>2
460 fprintf(stderr,
461 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
462 name, segment, offset, is_global, special);
463 #endif
464 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
466 * This is a NASM special symbol. We never allow it into
467 * the ELF symbol table, even if it's a valid one. If it
468 * _isn't_ a valid one, we should barf immediately.
470 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
471 strcmp(name, "..got") && strcmp(name, "..plt") &&
472 strcmp(name, "..sym"))
473 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
474 return;
477 if (is_global == 3) {
478 struct Symbol **s;
480 * Fix up a forward-reference symbol size from the first
481 * pass.
483 for (s = &fwds; *s; s = &(*s)->nextfwd)
484 if (!strcmp((*s)->name, name)) {
485 struct tokenval tokval;
486 expr *e;
487 char *p = special;
489 while (*p && !isspace(*p))
490 p++;
491 while (*p && isspace(*p))
492 p++;
493 stdscan_reset();
494 stdscan_bufptr = p;
495 tokval.t_type = TOKEN_INVALID;
496 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
497 if (e) {
498 if (!is_simple(e))
499 error(ERR_NONFATAL, "cannot use relocatable"
500 " expression as symbol size");
501 else
502 (*s)->size = reloc_value(e);
506 * Remove it from the list of unresolved sizes.
508 nasm_free((*s)->name);
509 *s = (*s)->nextfwd;
510 return;
512 return; /* it wasn't an important one */
515 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
516 strslen += 1 + strlen(name);
518 lastsym = sym = saa_wstruct(syms);
520 sym->strpos = pos;
521 sym->type = is_global ? SYM_GLOBAL : 0;
522 sym->other = STV_DEFAULT;
523 sym->size = 0;
524 if (segment == NO_SEG)
525 sym->section = SHN_ABS;
526 else {
527 int i;
528 sym->section = SHN_UNDEF;
529 if (nsects == 0 && segment == def_seg) {
530 int tempint;
531 if (segment != elf_section_names(".text", 2, &tempint))
532 error(ERR_PANIC,
533 "strange segment conditions in ELF driver");
534 sym->section = nsects;
535 } else {
536 for (i = 0; i < nsects; i++)
537 if (segment == sects[i]->index) {
538 sym->section = i + 1;
539 break;
544 if (is_global == 2) {
545 sym->size = offset;
546 sym->value = 0;
547 sym->section = SHN_COMMON;
549 * We have a common variable. Check the special text to see
550 * if it's a valid number and power of two; if so, store it
551 * as the alignment for the common variable.
553 if (special) {
554 bool err;
555 sym->value = readnum(special, &err);
556 if (err)
557 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
558 " valid number", special);
559 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
560 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
561 " power of two", special);
563 special_used = true;
564 } else
565 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
567 if (sym->type == SYM_GLOBAL) {
569 * If sym->section == SHN_ABS, then the first line of the
570 * else section would cause a core dump, because its a reference
571 * beyond the end of the section array.
572 * This behaviour is exhibited by this code:
573 * GLOBAL crash_nasm
574 * crash_nasm equ 0
575 * To avoid such a crash, such requests are silently discarded.
576 * This may not be the best solution.
578 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
579 bsym = raa_write(bsym, segment, nglobs);
580 } else if (sym->section != SHN_ABS) {
582 * This is a global symbol; so we must add it to the linked
583 * list of global symbols in its section. We'll push it on
584 * the beginning of the list, because it doesn't matter
585 * much which end we put it on and it's easier like this.
587 * In addition, we check the special text for symbol
588 * type and size information.
590 sym->next = sects[sym->section - 1]->gsyms;
591 sects[sym->section - 1]->gsyms = sym;
593 if (special) {
594 int n = strcspn(special, " \t");
596 if (!nasm_strnicmp(special, "function", n))
597 sym->type |= STT_FUNC;
598 else if (!nasm_strnicmp(special, "data", n) ||
599 !nasm_strnicmp(special, "object", n))
600 sym->type |= STT_OBJECT;
601 else if (!nasm_strnicmp(special, "notype", n))
602 sym->type |= STT_NOTYPE;
603 else
604 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
605 n, special);
606 special += n;
608 while (isspace(*special))
609 ++special;
610 if (*special) {
611 n = strcspn(special, " \t");
612 if (!nasm_strnicmp(special, "default", n))
613 sym->other = STV_DEFAULT;
614 else if (!nasm_strnicmp(special, "internal", n))
615 sym->other = STV_INTERNAL;
616 else if (!nasm_strnicmp(special, "hidden", n))
617 sym->other = STV_HIDDEN;
618 else if (!nasm_strnicmp(special, "protected", n))
619 sym->other = STV_PROTECTED;
620 else
621 n = 0;
622 special += n;
625 if (*special) {
626 struct tokenval tokval;
627 expr *e;
628 int fwd = 0;
629 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
631 while (special[n] && isspace(special[n]))
632 n++;
634 * We have a size expression; attempt to
635 * evaluate it.
637 stdscan_reset();
638 stdscan_bufptr = special + n;
639 tokval.t_type = TOKEN_INVALID;
640 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
641 NULL);
642 if (fwd) {
643 sym->nextfwd = fwds;
644 fwds = sym;
645 sym->name = nasm_strdup(name);
646 } else if (e) {
647 if (!is_simple(e))
648 error(ERR_NONFATAL, "cannot use relocatable"
649 " expression as symbol size");
650 else
651 sym->size = reloc_value(e);
653 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
655 special_used = true;
658 sym->globnum = nglobs;
659 nglobs++;
660 } else
661 nlocals++;
663 if (special && !special_used)
664 error(ERR_NONFATAL, "no special symbol features supported here");
667 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
669 struct Reloc *r;
671 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
672 sect->tail = &r->next;
673 r->next = NULL;
675 r->address = sect->len;
676 if (segment == NO_SEG)
677 r->symbol = 0;
678 else {
679 int i;
680 r->symbol = 0;
681 for (i = 0; i < nsects; i++)
682 if (segment == sects[i]->index)
683 r->symbol = i + 2;
684 if (!r->symbol)
685 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
687 r->type = type;
689 sect->nrelocs++;
693 * This routine deals with ..got and ..sym relocations: the more
694 * complicated kinds. In shared-library writing, some relocations
695 * with respect to global symbols must refer to the precise symbol
696 * rather than referring to an offset from the base of the section
697 * _containing_ the symbol. Such relocations call to this routine,
698 * which searches the symbol list for the symbol in question.
700 * R_386_GOT32 references require the _exact_ symbol address to be
701 * used; R_386_32 references can be at an offset from the symbol.
702 * The boolean argument `exact' tells us this.
704 * Return value is the adjusted value of `addr', having become an
705 * offset from the symbol rather than the section. Should always be
706 * zero when returning from an exact call.
708 * Limitation: if you define two symbols at the same place,
709 * confusion will occur.
711 * Inefficiency: we search, currently, using a linked list which
712 * isn't even necessarily sorted.
714 static int32_t elf_add_gsym_reloc(struct Section *sect,
715 int32_t segment, int32_t offset,
716 int type, bool exact)
718 struct Reloc *r;
719 struct Section *s;
720 struct Symbol *sym, *sm;
721 int i;
724 * First look up the segment/offset pair and find a global
725 * symbol corresponding to it. If it's not one of our segments,
726 * then it must be an external symbol, in which case we're fine
727 * doing a normal elf_add_reloc after first sanity-checking
728 * that the offset from the symbol is zero.
730 s = NULL;
731 for (i = 0; i < nsects; i++)
732 if (segment == sects[i]->index) {
733 s = sects[i];
734 break;
736 if (!s) {
737 if (exact && offset != 0)
738 error(ERR_NONFATAL, "unable to find a suitable global symbol"
739 " for this reference");
740 else
741 elf_add_reloc(sect, segment, type);
742 return offset;
745 if (exact) {
747 * Find a symbol pointing _exactly_ at this one.
749 for (sym = s->gsyms; sym; sym = sym->next)
750 if (sym->value == offset)
751 break;
752 } else {
754 * Find the nearest symbol below this one.
756 sym = NULL;
757 for (sm = s->gsyms; sm; sm = sm->next)
758 if (sm->value <= offset && (!sym || sm->value > sym->value))
759 sym = sm;
761 if (!sym && exact) {
762 error(ERR_NONFATAL, "unable to find a suitable global symbol"
763 " for this reference");
764 return 0;
767 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
768 sect->tail = &r->next;
769 r->next = NULL;
771 r->address = sect->len;
772 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
773 r->type = type;
775 sect->nrelocs++;
777 return offset - sym->value;
780 static void elf_out(int32_t segto, const void *data,
781 enum out_type type, uint64_t size,
782 int32_t segment, int32_t wrt)
784 struct Section *s;
785 int32_t addr;
786 uint8_t mydata[4], *p;
787 int i;
788 static struct symlininfo sinfo;
791 * handle absolute-assembly (structure definitions)
793 if (segto == NO_SEG) {
794 if (type != OUT_RESERVE)
795 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
796 " space");
797 return;
800 s = NULL;
801 for (i = 0; i < nsects; i++)
802 if (segto == sects[i]->index) {
803 s = sects[i];
804 break;
806 if (!s) {
807 int tempint; /* ignored */
808 if (segto != elf_section_names(".text", 2, &tempint))
809 error(ERR_PANIC, "strange segment conditions in ELF driver");
810 else {
811 s = sects[nsects - 1];
812 i = nsects - 1;
816 /* again some stabs debugging stuff */
817 if (of_elf32.current_dfmt) {
818 sinfo.offset = s->len;
819 sinfo.section = i;
820 sinfo.name = s->name;
821 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
823 /* end of debugging stuff */
825 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
826 error(ERR_WARNING, "attempt to initialize memory in"
827 " BSS section `%s': ignored", s->name);
828 if (type == OUT_REL2ADR)
829 size = 2;
830 else if (type == OUT_REL4ADR)
831 size = 4;
832 s->len += size;
833 return;
836 if (type == OUT_RESERVE) {
837 if (s->type == SHT_PROGBITS) {
838 error(ERR_WARNING, "uninitialized space declared in"
839 " non-BSS section `%s': zeroing", s->name);
840 elf_sect_write(s, NULL, size);
841 } else
842 s->len += size;
843 } else if (type == OUT_RAWDATA) {
844 if (segment != NO_SEG)
845 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
846 elf_sect_write(s, data, size);
847 } else if (type == OUT_ADDRESS) {
848 bool gnu16 = false;
849 addr = *(int64_t *)data;
850 if (segment != NO_SEG) {
851 if (segment % 2) {
852 error(ERR_NONFATAL, "ELF format does not support"
853 " segment base references");
854 } else {
855 if (wrt == NO_SEG) {
856 if (size == 2) {
857 gnu16 = true;
858 elf_add_reloc(s, segment, R_386_16);
859 } else {
860 elf_add_reloc(s, segment, R_386_32);
862 } else if (wrt == elf_gotpc_sect + 1) {
864 * The user will supply GOT relative to $$. ELF
865 * will let us have GOT relative to $. So we
866 * need to fix up the data item by $-$$.
868 addr += s->len;
869 elf_add_reloc(s, segment, R_386_GOTPC);
870 } else if (wrt == elf_gotoff_sect + 1) {
871 elf_add_reloc(s, segment, R_386_GOTOFF);
872 } else if (wrt == elf_got_sect + 1) {
873 addr = elf_add_gsym_reloc(s, segment, addr,
874 R_386_GOT32, true);
875 } else if (wrt == elf_sym_sect + 1) {
876 if (size == 2) {
877 gnu16 = true;
878 addr = elf_add_gsym_reloc(s, segment, addr,
879 R_386_16, false);
880 } else {
881 addr = elf_add_gsym_reloc(s, segment, addr,
882 R_386_32, false);
884 } else if (wrt == elf_plt_sect + 1) {
885 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
886 "relative PLT references");
887 } else {
888 error(ERR_NONFATAL, "ELF format does not support this"
889 " use of WRT");
890 wrt = NO_SEG; /* we can at least _try_ to continue */
894 p = mydata;
895 if (gnu16) {
896 error(ERR_WARNING | ERR_WARN_GNUELF,
897 "16-bit relocations in ELF is a GNU extension");
898 WRITESHORT(p, addr);
899 } else {
900 if (size != 4 && segment != NO_SEG) {
901 error(ERR_NONFATAL,
902 "Unsupported non-32-bit ELF relocation");
904 WRITELONG(p, addr);
906 elf_sect_write(s, mydata, size);
907 } else if (type == OUT_REL2ADR) {
908 if (segment == segto)
909 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
910 if (segment != NO_SEG && segment % 2) {
911 error(ERR_NONFATAL, "ELF format does not support"
912 " segment base references");
913 } else {
914 if (wrt == NO_SEG) {
915 error(ERR_WARNING | ERR_WARN_GNUELF,
916 "16-bit relocations in ELF is a GNU extension");
917 elf_add_reloc(s, segment, R_386_PC16);
918 } else {
919 error(ERR_NONFATAL,
920 "Unsupported non-32-bit ELF relocation");
923 p = mydata;
924 WRITESHORT(p, *(int64_t *)data - size);
925 elf_sect_write(s, mydata, 2L);
926 } else if (type == OUT_REL4ADR) {
927 if (segment == segto)
928 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
929 if (segment != NO_SEG && segment % 2) {
930 error(ERR_NONFATAL, "ELF format does not support"
931 " segment base references");
932 } else {
933 if (wrt == NO_SEG) {
934 elf_add_reloc(s, segment, R_386_PC32);
935 } else if (wrt == elf_plt_sect + 1) {
936 elf_add_reloc(s, segment, R_386_PLT32);
937 } else if (wrt == elf_gotpc_sect + 1 ||
938 wrt == elf_gotoff_sect + 1 ||
939 wrt == elf_got_sect + 1) {
940 error(ERR_NONFATAL, "ELF format cannot produce PC-"
941 "relative GOT references");
942 } else {
943 error(ERR_NONFATAL, "ELF format does not support this"
944 " use of WRT");
945 wrt = NO_SEG; /* we can at least _try_ to continue */
948 p = mydata;
949 WRITELONG(p, *(int64_t *)data - size);
950 elf_sect_write(s, mydata, 4L);
954 static void elf_write(void)
956 int nsections, align;
957 int scount;
958 char *p;
959 int commlen;
960 char comment[64];
961 int i;
963 struct SAA *symtab;
964 int32_t symtablen, symtablocal;
967 * Work out how many sections we will have. We have SHN_UNDEF,
968 * then the flexible user sections, then the four fixed
969 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
970 * then optionally relocation sections for the user sections.
972 if (of_elf32.current_dfmt == &df_stabs)
973 nsections = 8;
974 else
975 nsections = 5; /* SHN_UNDEF and the fixed ones */
977 add_sectname("", ".comment");
978 add_sectname("", ".shstrtab");
979 add_sectname("", ".symtab");
980 add_sectname("", ".strtab");
981 for (i = 0; i < nsects; i++) {
982 nsections++; /* for the section itself */
983 if (sects[i]->head) {
984 nsections++; /* for its relocations */
985 add_sectname(".rel", sects[i]->name);
989 if (of_elf32.current_dfmt == &df_stabs) {
990 /* in case the debug information is wanted, just add these three sections... */
991 add_sectname("", ".stab");
992 add_sectname("", ".stabstr");
993 add_sectname(".rel", ".stab");
997 * Do the comment.
999 *comment = '\0';
1000 commlen =
1001 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
1004 * Output the ELF header.
1006 fwrite("\177ELF\1\1\1", 7, 1, elffp);
1007 fputc(elf_osabi, elffp);
1008 fputc(elf_abiver, elffp);
1009 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp);
1010 fwriteint16_t(1, elffp); /* ET_REL relocatable file */
1011 fwriteint16_t(3, elffp); /* EM_386 processor ID */
1012 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1013 fwriteint32_t(0L, elffp); /* no entry point */
1014 fwriteint32_t(0L, elffp); /* no program header table */
1015 fwriteint32_t(0x40L, elffp); /* section headers straight after
1016 * ELF header plus alignment */
1017 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1018 fwriteint16_t(0x34, elffp); /* size of ELF header */
1019 fwriteint16_t(0, elffp); /* no program header table, again */
1020 fwriteint16_t(0, elffp); /* still no program header table */
1021 fwriteint16_t(0x28, elffp); /* size of section header */
1022 fwriteint16_t(nsections, elffp); /* number of sections */
1023 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1024 * section header table */
1025 fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
1026 fwriteint32_t(0L, elffp);
1027 fwriteint32_t(0L, elffp);
1030 * Build the symbol table and relocation tables.
1032 symtab = elf_build_symtab(&symtablen, &symtablocal);
1033 for (i = 0; i < nsects; i++)
1034 if (sects[i]->head)
1035 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1036 sects[i]->head);
1039 * Now output the section header table.
1042 elf_foffs = 0x40 + 0x28 * nsections;
1043 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1044 elf_foffs += align;
1045 elf_nsect = 0;
1046 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
1048 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1049 scount = 1; /* needed for the stabs debugging to track the symtable section */
1050 p = shstrtab + 1;
1051 for (i = 0; i < nsects; i++) {
1052 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1053 (sects[i]->type == SHT_PROGBITS ?
1054 sects[i]->data : NULL), true,
1055 sects[i]->len, 0, 0, sects[i]->align, 0);
1056 p += strlen(p) + 1;
1057 scount++; /* dito */
1059 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1060 scount++; /* dito */
1061 p += strlen(p) + 1;
1062 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1063 scount++; /* dito */
1064 p += strlen(p) + 1;
1065 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 16); /* .symtab */
1066 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1067 p += strlen(p) + 1;
1068 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1069 for (i = 0; i < nsects; i++)
1070 if (sects[i]->head) {
1071 p += strlen(p) + 1;
1072 elf_section_header(p - shstrtab, 9, 0, sects[i]->rel, true,
1073 sects[i]->rellen, nsects + 3, i + 1, 4, 8);
1075 if (of_elf32.current_dfmt == &df_stabs) {
1076 /* for debugging information, create the last three sections
1077 which are the .stab , .stabstr and .rel.stab sections respectively */
1079 /* this function call creates the stab sections in memory */
1080 stabs32_generate();
1082 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1083 p += strlen(p) + 1;
1084 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1085 nsections - 2, 0, 4, 12);
1087 p += strlen(p) + 1;
1088 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1089 stabstrlen, 0, 0, 4, 0);
1091 p += strlen(p) + 1;
1092 /* link -> symtable info -> section to refer to */
1093 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1094 stabrellen, symtabsection, nsections - 3, 4,
1098 fwrite(align_str, align, 1, elffp);
1101 * Now output the sections.
1103 elf_write_sections();
1105 nasm_free(elf_sects);
1106 saa_free(symtab);
1109 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1111 struct SAA *s = saa_init(1L);
1112 struct Symbol *sym;
1113 uint8_t entry[16], *p;
1114 int i;
1116 *len = *local = 0;
1119 * First, an all-zeros entry, required by the ELF spec.
1121 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1122 *len += 16;
1123 (*local)++;
1126 * Next, an entry for the file name.
1128 p = entry;
1129 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1130 WRITELONG(p, 0); /* no value */
1131 WRITELONG(p, 0); /* no size either */
1132 WRITESHORT(p, STT_FILE); /* type FILE */
1133 WRITESHORT(p, SHN_ABS);
1134 saa_wbytes(s, entry, 16L);
1135 *len += 16;
1136 (*local)++;
1139 * Now some standard symbols defining the segments, for relocation
1140 * purposes.
1142 for (i = 1; i <= nsects; i++) {
1143 p = entry;
1144 WRITELONG(p, 0); /* no symbol name */
1145 WRITELONG(p, 0); /* offset zero */
1146 WRITELONG(p, 0); /* size zero */
1147 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1148 WRITESHORT(p, i); /* section id */
1149 saa_wbytes(s, entry, 16L);
1150 *len += 16;
1151 (*local)++;
1155 * Now the other local symbols.
1157 saa_rewind(syms);
1158 while ((sym = saa_rstruct(syms))) {
1159 if (sym->type & SYM_GLOBAL)
1160 continue;
1161 p = entry;
1162 WRITELONG(p, sym->strpos);
1163 WRITELONG(p, sym->value);
1164 WRITELONG(p, sym->size);
1165 WRITECHAR(p, sym->type); /* type and binding */
1166 WRITECHAR(p, sym->other); /* visibility */
1167 WRITESHORT(p, sym->section);
1168 saa_wbytes(s, entry, 16L);
1169 *len += 16;
1170 (*local)++;
1174 * Now the global symbols.
1176 saa_rewind(syms);
1177 while ((sym = saa_rstruct(syms))) {
1178 if (!(sym->type & SYM_GLOBAL))
1179 continue;
1180 p = entry;
1181 WRITELONG(p, sym->strpos);
1182 WRITELONG(p, sym->value);
1183 WRITELONG(p, sym->size);
1184 WRITECHAR(p, sym->type); /* type and binding */
1185 WRITECHAR(p, sym->other); /* visibility */
1186 WRITESHORT(p, sym->section);
1187 saa_wbytes(s, entry, 16L);
1188 *len += 16;
1191 return s;
1194 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1196 struct SAA *s;
1197 uint8_t *p, entry[8];
1199 if (!r)
1200 return NULL;
1202 s = saa_init(1L);
1203 *len = 0;
1205 while (r) {
1206 int32_t sym = r->symbol;
1208 if (sym >= GLOBAL_TEMP_BASE)
1209 sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1211 p = entry;
1212 WRITELONG(p, r->address);
1213 WRITELONG(p, (sym << 8) + r->type);
1214 saa_wbytes(s, entry, 8L);
1215 *len += 8;
1217 r = r->next;
1220 return s;
1223 static void elf_section_header(int name, int type, int flags,
1224 void *data, bool is_saa, int32_t datalen,
1225 int link, int info, int align, int eltsize)
1227 elf_sects[elf_nsect].data = data;
1228 elf_sects[elf_nsect].len = datalen;
1229 elf_sects[elf_nsect].is_saa = is_saa;
1230 elf_nsect++;
1232 fwriteint32_t((int32_t)name, elffp);
1233 fwriteint32_t((int32_t)type, elffp);
1234 fwriteint32_t((int32_t)flags, elffp);
1235 fwriteint32_t(0L, elffp); /* no address, ever, in object files */
1236 fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
1237 fwriteint32_t(datalen, elffp);
1238 if (data)
1239 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1240 fwriteint32_t((int32_t)link, elffp);
1241 fwriteint32_t((int32_t)info, elffp);
1242 fwriteint32_t((int32_t)align, elffp);
1243 fwriteint32_t((int32_t)eltsize, elffp);
1246 static void elf_write_sections(void)
1248 int i;
1249 for (i = 0; i < elf_nsect; i++)
1250 if (elf_sects[i].data) {
1251 int32_t len = elf_sects[i].len;
1252 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1253 int32_t align = reallen - len;
1254 if (elf_sects[i].is_saa)
1255 saa_fpwrite(elf_sects[i].data, elffp);
1256 else
1257 fwrite(elf_sects[i].data, len, 1, elffp);
1258 fwrite(align_str, align, 1, elffp);
1262 static void elf_sect_write(struct Section *sect,
1263 const uint8_t *data, uint32_t len)
1265 saa_wbytes(sect->data, data, len);
1266 sect->len += len;
1269 static int32_t elf_segbase(int32_t segment)
1271 return segment;
1274 static int elf_directive(char *directive, char *value, int pass)
1276 bool err;
1277 int64_t n;
1278 char *p;
1280 if (!strcmp(directive, "osabi")) {
1281 if (pass == 2)
1282 return 1; /* ignore in pass 2 */
1284 n = readnum(value, &err);
1285 if (err) {
1286 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1287 return 1;
1289 if (n < 0 || n > 255) {
1290 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1291 return 1;
1293 elf_osabi = n;
1294 elf_abiver = 0;
1296 if ((p = strchr(value,',')) == NULL)
1297 return 1;
1299 n = readnum(p+1, &err);
1300 if (err || n < 0 || n > 255) {
1301 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1302 return 1;
1305 elf_abiver = n;
1306 return 1;
1309 return 0;
1312 static void elf_filename(char *inname, char *outname, efunc error)
1314 strcpy(elf_module, inname);
1315 standard_extension(inname, outname, ".o", error);
1318 static const char *elf_stdmac[] = {
1319 "%define __SECT__ [section .text]",
1320 "%macro __NASM_CDecl__ 1",
1321 "%define $_%1 $%1",
1322 "%endmacro",
1323 "%macro osabi 1+.nolist",
1324 "[osabi %1]",
1325 "%endmacro",
1326 NULL
1328 static int elf_set_info(enum geninfo type, char **val)
1330 (void)type;
1331 (void)val;
1332 return 0;
1335 static struct dfmt df_stabs = {
1336 "ELF32 (i386) stabs debug format for Linux",
1337 "stabs",
1338 stabs32_init,
1339 stabs32_linenum,
1340 stabs32_deflabel,
1341 stabs32_directive,
1342 stabs32_typevalue,
1343 stabs32_output,
1344 stabs32_cleanup
1347 struct dfmt *elf32_debugs_arr[2] = { &df_stabs, NULL };
1349 struct ofmt of_elf32 = {
1350 "ELF32 (i386) object files (e.g. Linux)",
1351 "elf32",
1352 NULL,
1353 elf32_debugs_arr,
1354 &null_debug_form,
1355 elf_stdmac,
1356 elf_init,
1357 elf_set_info,
1358 elf_out,
1359 elf_deflabel,
1360 elf_section_names,
1361 elf_segbase,
1362 elf_directive,
1363 elf_filename,
1364 elf_cleanup
1367 struct ofmt of_elf = {
1368 "ELF (short name for ELF32) ",
1369 "elf",
1370 NULL,
1371 elf32_debugs_arr,
1372 &null_debug_form,
1373 elf_stdmac,
1374 elf_init,
1375 elf_set_info,
1376 elf_out,
1377 elf_deflabel,
1378 elf_section_names,
1379 elf_segbase,
1380 elf_directive,
1381 elf_filename,
1382 elf_cleanup
1384 /* again, the stabs debugging stuff (code) */
1386 void stabs32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1388 (void)of;
1389 (void)id;
1390 (void)fp;
1391 (void)error;
1394 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1396 (void)segto;
1398 if (!stabs_filename) {
1399 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1400 strcpy(stabs_filename, filename);
1401 } else {
1402 if (strcmp(stabs_filename, filename)) {
1403 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1404 in fact, this leak comes in quite handy to maintain a list of files
1405 encountered so far in the symbol lines... */
1407 /* why not nasm_free(stabs_filename); we're done with the old one */
1409 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1410 strcpy(stabs_filename, filename);
1413 stabs_immcall = 1;
1414 currentline = linenumber;
1417 void stabs32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1418 char *special)
1420 (void)name;
1421 (void)segment;
1422 (void)offset;
1423 (void)is_global;
1424 (void)special;
1427 void stabs32_directive(const char *directive, const char *params)
1429 (void)directive;
1430 (void)params;
1433 void stabs32_typevalue(int32_t type)
1435 int32_t stype, ssize;
1436 switch (TYM_TYPE(type)) {
1437 case TY_LABEL:
1438 ssize = 0;
1439 stype = STT_NOTYPE;
1440 break;
1441 case TY_BYTE:
1442 ssize = 1;
1443 stype = STT_OBJECT;
1444 break;
1445 case TY_WORD:
1446 ssize = 2;
1447 stype = STT_OBJECT;
1448 break;
1449 case TY_DWORD:
1450 ssize = 4;
1451 stype = STT_OBJECT;
1452 break;
1453 case TY_FLOAT:
1454 ssize = 4;
1455 stype = STT_OBJECT;
1456 break;
1457 case TY_QWORD:
1458 ssize = 8;
1459 stype = STT_OBJECT;
1460 break;
1461 case TY_TBYTE:
1462 ssize = 10;
1463 stype = STT_OBJECT;
1464 break;
1465 case TY_OWORD:
1466 ssize = 8;
1467 stype = STT_OBJECT;
1468 break;
1469 case TY_COMMON:
1470 ssize = 0;
1471 stype = STT_COMMON;
1472 break;
1473 case TY_SEG:
1474 ssize = 0;
1475 stype = STT_SECTION;
1476 break;
1477 case TY_EXTERN:
1478 ssize = 0;
1479 stype = STT_NOTYPE;
1480 break;
1481 case TY_EQU:
1482 ssize = 0;
1483 stype = STT_NOTYPE;
1484 break;
1485 default:
1486 ssize = 0;
1487 stype = STT_NOTYPE;
1488 break;
1490 if (stype == STT_OBJECT && !lastsym->type) {
1491 lastsym->size = ssize;
1492 lastsym->type = stype;
1496 void stabs32_output(int type, void *param)
1498 struct symlininfo *s;
1499 struct linelist *el;
1500 if (type == TY_STABSSYMLIN) {
1501 if (stabs_immcall) {
1502 s = (struct symlininfo *)param;
1503 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1504 return; /* we are only interested in the text stuff */
1505 numlinestabs++;
1506 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1507 el->info.offset = s->offset;
1508 el->info.section = s->section;
1509 el->info.name = s->name;
1510 el->line = currentline;
1511 el->filename = stabs_filename;
1512 el->next = 0;
1513 if (stabslines) {
1514 stabslines->last->next = el;
1515 stabslines->last = el;
1516 } else {
1517 stabslines = el;
1518 stabslines->last = el;
1522 stabs_immcall = 0;
1525 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1526 do {\
1527 WRITELONG(p,n_strx); \
1528 WRITECHAR(p,n_type); \
1529 WRITECHAR(p,n_other); \
1530 WRITESHORT(p,n_desc); \
1531 WRITELONG(p,n_value); \
1532 } while (0)
1534 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1536 void stabs32_generate(void)
1538 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1539 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1540 char **allfiles;
1541 int *fileidx;
1543 struct linelist *ptr;
1545 ptr = stabslines;
1547 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1548 for (i = 0; i < numlinestabs; i++)
1549 allfiles[i] = 0;
1550 numfiles = 0;
1551 while (ptr) {
1552 if (numfiles == 0) {
1553 allfiles[0] = ptr->filename;
1554 numfiles++;
1555 } else {
1556 for (i = 0; i < numfiles; i++) {
1557 if (!strcmp(allfiles[i], ptr->filename))
1558 break;
1560 if (i >= numfiles) {
1561 allfiles[i] = ptr->filename;
1562 numfiles++;
1565 ptr = ptr->next;
1567 strsize = 1;
1568 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1569 for (i = 0; i < numfiles; i++) {
1570 fileidx[i] = strsize;
1571 strsize += strlen(allfiles[i]) + 1;
1573 mainfileindex = 0;
1574 for (i = 0; i < numfiles; i++) {
1575 if (!strcmp(allfiles[i], elf_module)) {
1576 mainfileindex = i;
1577 break;
1581 /* worst case size of the stab buffer would be:
1582 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1584 sbuf =
1585 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1586 sizeof(struct stabentry));
1588 ssbuf = (uint8_t *)nasm_malloc(strsize);
1590 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1591 rptr = rbuf;
1593 for (i = 0; i < numfiles; i++) {
1594 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1596 ssbuf[0] = 0;
1598 stabstrlen = strsize; /* set global variable for length of stab strings */
1600 sptr = sbuf;
1601 ptr = stabslines;
1602 numstabs = 0;
1604 if (ptr) {
1605 /* this is the first stab, its strx points to the filename of the
1606 the source-file, the n_desc field should be set to the number
1607 of remaining stabs
1609 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1611 /* this is the stab for the main source file */
1612 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1614 /* relocation table entry */
1616 /* Since the symbol table has two entries before */
1617 /* the section symbols, the index in the info.section */
1618 /* member must be adjusted by adding 2 */
1620 WRITELONG(rptr, (sptr - sbuf) - 4);
1621 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1623 numstabs++;
1624 currfile = mainfileindex;
1627 while (ptr) {
1628 if (strcmp(allfiles[currfile], ptr->filename)) {
1629 /* oops file has changed... */
1630 for (i = 0; i < numfiles; i++)
1631 if (!strcmp(allfiles[i], ptr->filename))
1632 break;
1633 currfile = i;
1634 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1635 ptr->info.offset);
1636 numstabs++;
1638 /* relocation table entry */
1639 WRITELONG(rptr, (sptr - sbuf) - 4);
1640 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1643 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1644 numstabs++;
1646 /* relocation table entry */
1648 WRITELONG(rptr, (sptr - sbuf) - 4);
1649 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1651 ptr = ptr->next;
1655 ((struct stabentry *)sbuf)->n_desc = numstabs;
1657 nasm_free(allfiles);
1658 nasm_free(fileidx);
1660 stablen = (sptr - sbuf);
1661 stabrellen = (rptr - rbuf);
1662 stabrelbuf = rbuf;
1663 stabbuf = sbuf;
1664 stabstrbuf = ssbuf;
1667 void stabs32_cleanup(void)
1669 struct linelist *ptr, *del;
1670 if (!stabslines)
1671 return;
1672 ptr = stabslines;
1673 while (ptr) {
1674 del = ptr;
1675 ptr = ptr->next;
1676 nasm_free(del);
1678 if (stabbuf)
1679 nasm_free(stabbuf);
1680 if (stabrelbuf)
1681 nasm_free(stabrelbuf);
1682 if (stabstrbuf)
1683 nasm_free(stabstrbuf);
1686 #endif /* OF_ELF */