Fix memory management issues with expanded %include
[nasm/avx512.git] / output / outelf32.c
blob491b9ae4e4c7fe91711aa036cb02a72d567bb772
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 license given in the file "LICENSE"
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"
22 #include "wsaa.h"
24 #ifdef OF_ELF32
27 * Relocation types.
29 enum reloc_type {
30 R_386_32 = 1, /* ordinary absolute relocation */
31 R_386_PC32 = 2, /* PC-relative relocation */
32 R_386_GOT32 = 3, /* an offset into GOT */
33 R_386_PLT32 = 4, /* a PC-relative offset into PLT */
34 R_386_COPY = 5, /* ??? */
35 R_386_GLOB_DAT = 6, /* ??? */
36 R_386_JUMP_SLOT = 7, /* ??? */
37 R_386_RELATIVE = 8, /* ??? */
38 R_386_GOTOFF = 9, /* an offset from GOT base */
39 R_386_GOTPC = 10, /* a PC-relative offset _to_ GOT */
40 /* These are GNU extensions, but useful */
41 R_386_16 = 20, /* A 16-bit absolute relocation */
42 R_386_PC16 = 21, /* A 16-bit PC-relative relocation */
43 R_386_8 = 22, /* An 8-bit absolute relocation */
44 R_386_PC8 = 23 /* An 8-bit PC-relative relocation */
47 struct Reloc {
48 struct Reloc *next;
49 int32_t address; /* relative to _start_ of section */
50 int32_t symbol; /* symbol index */
51 int type; /* type of relocation */
54 struct Symbol {
55 int32_t strpos; /* string table position of name */
56 int32_t section; /* section ID of the symbol */
57 int type; /* symbol type */
58 int other; /* symbol visibility */
59 int32_t value; /* address, or COMMON variable align */
60 int32_t size; /* size of symbol */
61 int32_t globnum; /* symbol table offset if global */
62 struct Symbol *next; /* list of globals in each section */
63 struct Symbol *nextfwd; /* list of unresolved-size symbols */
64 char *name; /* used temporarily if in above list */
67 #define SHT_PROGBITS 1
68 #define SHT_NOBITS 8
70 #define SHF_WRITE 1
71 #define SHF_ALLOC 2
72 #define SHF_EXECINSTR 4
74 struct Section {
75 struct SAA *data;
76 uint32_t len, size, nrelocs;
77 int32_t index;
78 int type; /* SHT_PROGBITS or SHT_NOBITS */
79 int align; /* alignment: power of two */
80 uint32_t flags; /* section flags */
81 char *name;
82 struct SAA *rel;
83 int32_t rellen;
84 struct Reloc *head, **tail;
85 struct Symbol *gsyms; /* global symbols in section */
88 #define SECT_DELTA 32
89 static struct Section **sects;
90 static int nsects, sectlen;
92 #define SHSTR_DELTA 256
93 static char *shstrtab;
94 static int shstrtablen, shstrtabsize;
96 static struct SAA *syms;
97 static uint32_t nlocals, nglobs;
99 static int32_t def_seg;
101 static struct RAA *bsym;
103 static struct SAA *strs;
104 static uint32_t strslen;
106 static FILE *elffp;
107 static efunc error;
108 static evalfunc evaluate;
110 static struct Symbol *fwds;
112 static char elf_module[FILENAME_MAX];
114 static uint8_t elf_osabi = 0; /* Default OSABI = 0 (System V or Linux) */
115 static uint8_t elf_abiver = 0; /* Current ABI version */
117 extern struct ofmt of_elf32;
118 extern struct ofmt of_elf;
120 #define SHN_ABS 0xFFF1
121 #define SHN_COMMON 0xFFF2
122 #define SHN_UNDEF 0
124 #define SYM_GLOBAL 0x10
126 #define SHT_RELA 4 /* Relocation entries with addends */
128 #define STT_NOTYPE 0 /* Symbol type is unspecified */
129 #define STT_OBJECT 1 /* Symbol is a data object */
130 #define STT_FUNC 2 /* Symbol is a code object */
131 #define STT_SECTION 3 /* Symbol associated with a section */
132 #define STT_FILE 4 /* Symbol's name is file name */
133 #define STT_COMMON 5 /* Symbol is a common data object */
134 #define STT_TLS 6 /* Symbol is thread-local data object*/
135 #define STT_NUM 7 /* Number of defined types. */
137 #define STV_DEFAULT 0
138 #define STV_INTERNAL 1
139 #define STV_HIDDEN 2
140 #define STV_PROTECTED 3
142 #define GLOBAL_TEMP_BASE 1048576 /* bigger than any reasonable sym id */
144 #define SEG_ALIGN 16 /* alignment of sections in file */
145 #define SEG_ALIGN_1 (SEG_ALIGN-1)
147 /* Definitions in lieu of dwarf.h */
148 #define DW_TAG_compile_unit 0x11
149 #define DW_TAG_subprogram 0x2e
150 #define DW_AT_name 0x03
151 #define DW_AT_stmt_list 0x10
152 #define DW_AT_low_pc 0x11
153 #define DW_AT_high_pc 0x12
154 #define DW_AT_language 0x13
155 #define DW_AT_producer 0x25
156 #define DW_AT_frame_base 0x40
157 #define DW_FORM_addr 0x01
158 #define DW_FORM_data2 0x05
159 #define DW_FORM_data4 0x06
160 #define DW_FORM_string 0x08
161 #define DW_LNS_extended_op 0
162 #define DW_LNS_advance_pc 2
163 #define DW_LNS_advance_line 3
164 #define DW_LNS_set_file 4
165 #define DW_LNE_end_sequence 1
166 #define DW_LNE_set_address 2
167 #define DW_LNE_define_file 3
168 #define DW_LANG_Mips_Assembler 0x8001
170 #define SOC(ln,aa) ln - line_base + (line_range * aa) + opcode_base
172 static const char align_str[SEG_ALIGN] = ""; /* ANSI will pad this with 0s */
174 static struct ELF_SECTDATA {
175 void *data;
176 int32_t len;
177 bool is_saa;
178 } *elf_sects;
179 static int elf_nsect, nsections;
180 static int32_t elf_foffs;
182 static void elf_write(void);
183 static void elf_sect_write(struct Section *, const uint8_t *,
184 uint32_t);
185 static void elf_section_header(int, int, int, void *, bool, int32_t, int, int,
186 int, int);
187 static void elf_write_sections(void);
188 static struct SAA *elf_build_symtab(int32_t *, int32_t *);
189 static struct SAA *elf_build_reltab(int32_t *, struct Reloc *);
190 static void add_sectname(char *, char *);
192 /* this stuff is needed for the stabs debugging format */
193 #define N_SO 0x64 /* ID for main source file */
194 #define N_SOL 0x84 /* ID for sub-source file */
195 #define N_BINCL 0x82
196 #define N_EINCL 0xA2
197 #define N_SLINE 0x44
198 #define TY_STABSSYMLIN 0x40 /* ouch */
200 struct stabentry {
201 uint32_t n_strx;
202 uint8_t n_type;
203 uint8_t n_other;
204 uint16_t n_desc;
205 uint32_t n_value;
208 struct erel {
209 int offset, info;
212 struct symlininfo {
213 int offset;
214 int section; /* section index */
215 char *name; /* shallow-copied pointer of section name */
218 struct linelist {
219 struct symlininfo info;
220 int line;
221 char *filename;
222 struct linelist *next;
223 struct linelist *last;
226 struct sectlist {
227 struct SAA *psaa;
228 int section;
229 int line;
230 int offset;
231 int file;
232 struct sectlist *next;
233 struct sectlist *last;
236 /* common debug variables */
237 static int currentline = 1;
238 static int debug_immcall = 0;
240 /* stabs debug variables */
241 static struct linelist *stabslines = 0;
242 static int numlinestabs = 0;
243 static char *stabs_filename = 0;
244 static int symtabsection;
245 static uint8_t *stabbuf = 0, *stabstrbuf = 0, *stabrelbuf = 0;
246 static int stablen, stabstrlen, stabrellen;
248 /* dwarf debug variables */
249 static struct linelist *dwarf_flist = 0, *dwarf_clist = 0, *dwarf_elist = 0;
250 static struct sectlist *dwarf_fsect = 0, *dwarf_csect = 0, *dwarf_esect = 0;
251 static int dwarf_numfiles = 0, dwarf_nsections;
252 static uint8_t *arangesbuf = 0, *arangesrelbuf = 0, *pubnamesbuf = 0, *infobuf = 0, *inforelbuf = 0,
253 *abbrevbuf = 0, *linebuf = 0, *linerelbuf = 0, *framebuf = 0, *locbuf = 0;
254 static int8_t line_base = -5, line_range = 14, opcode_base = 13;
255 static int arangeslen, arangesrellen, pubnameslen, infolen, inforellen,
256 abbrevlen, linelen, linerellen, framelen, loclen;
257 static int32_t dwarf_infosym, dwarf_abbrevsym, dwarf_linesym;
258 static char workbuf[1024];
260 static struct dfmt df_dwarf;
261 static struct dfmt df_stabs;
262 static struct Symbol *lastsym;
264 /* common debugging routines */
265 void debug32_typevalue(int32_t);
266 void debug32_init(struct ofmt *, void *, FILE *, efunc);
267 void debug32_deflabel(char *, int32_t, int64_t, int, char *);
268 void debug32_directive(const char *, const char *);
270 /* stabs debugging routines */
271 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t);
272 void stabs32_output(int, void *);
273 void stabs32_generate(void);
274 void stabs32_cleanup(void);
276 /* dwarf debugging routines */
277 void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t);
278 void dwarf32_output(int, void *);
279 void dwarf32_generate(void);
280 void dwarf32_cleanup(void);
281 void dwarf32_findfile(const char *);
282 void dwarf32_findsect(const int);
283 void saa_wleb128u(struct SAA *, int);
284 void saa_wleb128s(struct SAA *, int);
287 * Special section numbers which are used to define ELF special
288 * symbols, which can be used with WRT to provide PIC relocation
289 * types.
291 static int32_t elf_gotpc_sect, elf_gotoff_sect;
292 static int32_t elf_got_sect, elf_plt_sect;
293 static int32_t elf_sym_sect;
295 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
297 if (of_elf.current_dfmt != &null_debug_form)
298 of_elf32.current_dfmt = of_elf.current_dfmt;
299 elffp = fp;
300 error = errfunc;
301 evaluate = eval;
302 (void)ldef; /* placate optimisers */
303 sects = NULL;
304 nsects = sectlen = 0;
305 syms = saa_init((int32_t)sizeof(struct Symbol));
306 nlocals = nglobs = 0;
307 bsym = raa_init();
308 strs = saa_init(1L);
309 saa_wbytes(strs, "\0", 1L);
310 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
311 strslen = 2 + strlen(elf_module);
312 shstrtab = NULL;
313 shstrtablen = shstrtabsize = 0;;
314 add_sectname("", "");
316 fwds = NULL;
318 elf_gotpc_sect = seg_alloc();
319 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, false, false, &of_elf32,
320 error);
321 elf_gotoff_sect = seg_alloc();
322 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, false, false, &of_elf32,
323 error);
324 elf_got_sect = seg_alloc();
325 ldef("..got", elf_got_sect + 1, 0L, NULL, false, false, &of_elf32,
326 error);
327 elf_plt_sect = seg_alloc();
328 ldef("..plt", elf_plt_sect + 1, 0L, NULL, false, false, &of_elf32,
329 error);
330 elf_sym_sect = seg_alloc();
331 ldef("..sym", elf_sym_sect + 1, 0L, NULL, false, false, &of_elf32,
332 error);
334 def_seg = seg_alloc();
337 static void elf_cleanup(int debuginfo)
339 struct Reloc *r;
340 int i;
342 (void)debuginfo;
344 elf_write();
345 fclose(elffp);
346 for (i = 0; i < nsects; i++) {
347 if (sects[i]->type != SHT_NOBITS)
348 saa_free(sects[i]->data);
349 if (sects[i]->head)
350 saa_free(sects[i]->rel);
351 while (sects[i]->head) {
352 r = sects[i]->head;
353 sects[i]->head = sects[i]->head->next;
354 nasm_free(r);
357 nasm_free(sects);
358 saa_free(syms);
359 raa_free(bsym);
360 saa_free(strs);
361 if (of_elf32.current_dfmt) {
362 of_elf32.current_dfmt->cleanup();
366 static void add_sectname(char *firsthalf, char *secondhalf)
368 int len = strlen(firsthalf) + strlen(secondhalf);
369 while (shstrtablen + len + 1 > shstrtabsize)
370 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
371 strcpy(shstrtab + shstrtablen, firsthalf);
372 strcat(shstrtab + shstrtablen, secondhalf);
373 shstrtablen += len + 1;
376 static int elf_make_section(char *name, int type, int flags, int align)
378 struct Section *s;
380 s = nasm_malloc(sizeof(*s));
382 if (type != SHT_NOBITS)
383 s->data = saa_init(1L);
384 s->head = NULL;
385 s->tail = &s->head;
386 s->len = s->size = 0;
387 s->nrelocs = 0;
388 if (!strcmp(name, ".text"))
389 s->index = def_seg;
390 else
391 s->index = seg_alloc();
392 add_sectname("", name);
393 s->name = nasm_malloc(1 + strlen(name));
394 strcpy(s->name, name);
395 s->type = type;
396 s->flags = flags;
397 s->align = align;
398 s->gsyms = NULL;
400 if (nsects >= sectlen)
401 sects =
402 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
403 sects[nsects++] = s;
405 return nsects - 1;
408 static int32_t elf_section_names(char *name, int pass, int *bits)
410 char *p;
411 unsigned flags_and, flags_or;
412 int type, align, i;
415 * Default is 32 bits.
417 if (!name) {
418 *bits = 32;
419 return def_seg;
422 p = name;
423 while (*p && !isspace(*p))
424 p++;
425 if (*p)
426 *p++ = '\0';
427 flags_and = flags_or = type = align = 0;
429 while (*p && isspace(*p))
430 p++;
431 while (*p) {
432 char *q = p;
433 while (*p && !isspace(*p))
434 p++;
435 if (*p)
436 *p++ = '\0';
437 while (*p && isspace(*p))
438 p++;
440 if (!nasm_strnicmp(q, "align=", 6)) {
441 align = atoi(q + 6);
442 if (align == 0)
443 align = 1;
444 if ((align - 1) & align) { /* means it's not a power of two */
445 error(ERR_NONFATAL, "section alignment %d is not"
446 " a power of two", align);
447 align = 1;
449 } else if (!nasm_stricmp(q, "alloc")) {
450 flags_and |= SHF_ALLOC;
451 flags_or |= SHF_ALLOC;
452 } else if (!nasm_stricmp(q, "noalloc")) {
453 flags_and |= SHF_ALLOC;
454 flags_or &= ~SHF_ALLOC;
455 } else if (!nasm_stricmp(q, "exec")) {
456 flags_and |= SHF_EXECINSTR;
457 flags_or |= SHF_EXECINSTR;
458 } else if (!nasm_stricmp(q, "noexec")) {
459 flags_and |= SHF_EXECINSTR;
460 flags_or &= ~SHF_EXECINSTR;
461 } else if (!nasm_stricmp(q, "write")) {
462 flags_and |= SHF_WRITE;
463 flags_or |= SHF_WRITE;
464 } else if (!nasm_stricmp(q, "nowrite")) {
465 flags_and |= SHF_WRITE;
466 flags_or &= ~SHF_WRITE;
467 } else if (!nasm_stricmp(q, "progbits")) {
468 type = SHT_PROGBITS;
469 } else if (!nasm_stricmp(q, "nobits")) {
470 type = SHT_NOBITS;
474 if (!strcmp(name, ".comment") ||
475 !strcmp(name, ".shstrtab") ||
476 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
477 error(ERR_NONFATAL, "attempt to redefine reserved section"
478 "name `%s'", name);
479 return NO_SEG;
482 for (i = 0; i < nsects; i++)
483 if (!strcmp(name, sects[i]->name))
484 break;
485 if (i == nsects) {
486 if (!strcmp(name, ".text"))
487 i = elf_make_section(name, SHT_PROGBITS,
488 SHF_ALLOC | SHF_EXECINSTR, 16);
489 else if (!strcmp(name, ".rodata"))
490 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
491 else if (!strcmp(name, ".data"))
492 i = elf_make_section(name, SHT_PROGBITS,
493 SHF_ALLOC | SHF_WRITE, 4);
494 else if (!strcmp(name, ".bss"))
495 i = elf_make_section(name, SHT_NOBITS,
496 SHF_ALLOC | SHF_WRITE, 4);
497 else
498 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
499 if (type)
500 sects[i]->type = type;
501 if (align)
502 sects[i]->align = align;
503 sects[i]->flags &= ~flags_and;
504 sects[i]->flags |= flags_or;
505 } else if (pass == 1) {
506 if ((type && sects[i]->type != type)
507 || (align && sects[i]->align != align)
508 || (flags_and && ((sects[i]->flags & flags_and) != flags_or)))
509 error(ERR_WARNING, "section attributes ignored on"
510 " redeclaration of section `%s'", name);
513 return sects[i]->index;
516 static void elf_deflabel(char *name, int32_t segment, int64_t offset,
517 int is_global, char *special)
519 int pos = strslen;
520 struct Symbol *sym;
521 bool special_used = false;
523 #if defined(DEBUG) && DEBUG>2
524 fprintf(stderr,
525 " elf_deflabel: %s, seg=%ld, off=%ld, is_global=%d, %s\n",
526 name, segment, offset, is_global, special);
527 #endif
528 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
530 * This is a NASM special symbol. We never allow it into
531 * the ELF symbol table, even if it's a valid one. If it
532 * _isn't_ a valid one, we should barf immediately.
534 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
535 strcmp(name, "..got") && strcmp(name, "..plt") &&
536 strcmp(name, "..sym"))
537 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
538 return;
541 if (is_global == 3) {
542 struct Symbol **s;
544 * Fix up a forward-reference symbol size from the first
545 * pass.
547 for (s = &fwds; *s; s = &(*s)->nextfwd)
548 if (!strcmp((*s)->name, name)) {
549 struct tokenval tokval;
550 expr *e;
551 char *p = special;
553 while (*p && !isspace(*p))
554 p++;
555 while (*p && isspace(*p))
556 p++;
557 stdscan_reset();
558 stdscan_bufptr = p;
559 tokval.t_type = TOKEN_INVALID;
560 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
561 if (e) {
562 if (!is_simple(e))
563 error(ERR_NONFATAL, "cannot use relocatable"
564 " expression as symbol size");
565 else
566 (*s)->size = reloc_value(e);
570 * Remove it from the list of unresolved sizes.
572 nasm_free((*s)->name);
573 *s = (*s)->nextfwd;
574 return;
576 return; /* it wasn't an important one */
579 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
580 strslen += 1 + strlen(name);
582 lastsym = sym = saa_wstruct(syms);
584 sym->strpos = pos;
585 sym->type = is_global ? SYM_GLOBAL : 0;
586 sym->other = STV_DEFAULT;
587 sym->size = 0;
588 if (segment == NO_SEG)
589 sym->section = SHN_ABS;
590 else {
591 int i;
592 sym->section = SHN_UNDEF;
593 if (nsects == 0 && segment == def_seg) {
594 int tempint;
595 if (segment != elf_section_names(".text", 2, &tempint))
596 error(ERR_PANIC,
597 "strange segment conditions in ELF driver");
598 sym->section = nsects;
599 } else {
600 for (i = 0; i < nsects; i++)
601 if (segment == sects[i]->index) {
602 sym->section = i + 1;
603 break;
608 if (is_global == 2) {
609 sym->size = offset;
610 sym->value = 0;
611 sym->section = SHN_COMMON;
613 * We have a common variable. Check the special text to see
614 * if it's a valid number and power of two; if so, store it
615 * as the alignment for the common variable.
617 if (special) {
618 bool err;
619 sym->value = readnum(special, &err);
620 if (err)
621 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
622 " valid number", special);
623 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
624 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
625 " power of two", special);
627 special_used = true;
628 } else
629 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
631 if (sym->type == SYM_GLOBAL) {
633 * If sym->section == SHN_ABS, then the first line of the
634 * else section would cause a core dump, because its a reference
635 * beyond the end of the section array.
636 * This behaviour is exhibited by this code:
637 * GLOBAL crash_nasm
638 * crash_nasm equ 0
639 * To avoid such a crash, such requests are silently discarded.
640 * This may not be the best solution.
642 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
643 bsym = raa_write(bsym, segment, nglobs);
644 } else if (sym->section != SHN_ABS) {
646 * This is a global symbol; so we must add it to the linked
647 * list of global symbols in its section. We'll push it on
648 * the beginning of the list, because it doesn't matter
649 * much which end we put it on and it's easier like this.
651 * In addition, we check the special text for symbol
652 * type and size information.
654 sym->next = sects[sym->section - 1]->gsyms;
655 sects[sym->section - 1]->gsyms = sym;
657 if (special) {
658 int n = strcspn(special, " \t");
660 if (!nasm_strnicmp(special, "function", n))
661 sym->type |= STT_FUNC;
662 else if (!nasm_strnicmp(special, "data", n) ||
663 !nasm_strnicmp(special, "object", n))
664 sym->type |= STT_OBJECT;
665 else if (!nasm_strnicmp(special, "notype", n))
666 sym->type |= STT_NOTYPE;
667 else
668 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
669 n, special);
670 special += n;
672 while (isspace(*special))
673 ++special;
674 if (*special) {
675 n = strcspn(special, " \t");
676 if (!nasm_strnicmp(special, "default", n))
677 sym->other = STV_DEFAULT;
678 else if (!nasm_strnicmp(special, "internal", n))
679 sym->other = STV_INTERNAL;
680 else if (!nasm_strnicmp(special, "hidden", n))
681 sym->other = STV_HIDDEN;
682 else if (!nasm_strnicmp(special, "protected", n))
683 sym->other = STV_PROTECTED;
684 else
685 n = 0;
686 special += n;
689 if (*special) {
690 struct tokenval tokval;
691 expr *e;
692 int fwd = 0;
693 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
695 while (special[n] && isspace(special[n]))
696 n++;
698 * We have a size expression; attempt to
699 * evaluate it.
701 stdscan_reset();
702 stdscan_bufptr = special + n;
703 tokval.t_type = TOKEN_INVALID;
704 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
705 NULL);
706 if (fwd) {
707 sym->nextfwd = fwds;
708 fwds = sym;
709 sym->name = nasm_strdup(name);
710 } else if (e) {
711 if (!is_simple(e))
712 error(ERR_NONFATAL, "cannot use relocatable"
713 " expression as symbol size");
714 else
715 sym->size = reloc_value(e);
717 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
719 special_used = true;
722 sym->globnum = nglobs;
723 nglobs++;
724 } else
725 nlocals++;
727 if (special && !special_used)
728 error(ERR_NONFATAL, "no special symbol features supported here");
731 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
733 struct Reloc *r;
735 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
736 sect->tail = &r->next;
737 r->next = NULL;
739 r->address = sect->len;
740 if (segment == NO_SEG)
741 r->symbol = 0;
742 else {
743 int i;
744 r->symbol = 0;
745 for (i = 0; i < nsects; i++)
746 if (segment == sects[i]->index)
747 r->symbol = i + 2;
748 if (!r->symbol)
749 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
751 r->type = type;
753 sect->nrelocs++;
757 * This routine deals with ..got and ..sym relocations: the more
758 * complicated kinds. In shared-library writing, some relocations
759 * with respect to global symbols must refer to the precise symbol
760 * rather than referring to an offset from the base of the section
761 * _containing_ the symbol. Such relocations call to this routine,
762 * which searches the symbol list for the symbol in question.
764 * R_386_GOT32 references require the _exact_ symbol address to be
765 * used; R_386_32 references can be at an offset from the symbol.
766 * The boolean argument `exact' tells us this.
768 * Return value is the adjusted value of `addr', having become an
769 * offset from the symbol rather than the section. Should always be
770 * zero when returning from an exact call.
772 * Limitation: if you define two symbols at the same place,
773 * confusion will occur.
775 * Inefficiency: we search, currently, using a linked list which
776 * isn't even necessarily sorted.
778 static int32_t elf_add_gsym_reloc(struct Section *sect,
779 int32_t segment, int32_t offset,
780 int type, bool exact)
782 struct Reloc *r;
783 struct Section *s;
784 struct Symbol *sym, *sm;
785 int i;
788 * First look up the segment/offset pair and find a global
789 * symbol corresponding to it. If it's not one of our segments,
790 * then it must be an external symbol, in which case we're fine
791 * doing a normal elf_add_reloc after first sanity-checking
792 * that the offset from the symbol is zero.
794 s = NULL;
795 for (i = 0; i < nsects; i++)
796 if (segment == sects[i]->index) {
797 s = sects[i];
798 break;
800 if (!s) {
801 if (exact && offset != 0)
802 error(ERR_NONFATAL, "unable to find a suitable global symbol"
803 " for this reference");
804 else
805 elf_add_reloc(sect, segment, type);
806 return offset;
809 if (exact) {
811 * Find a symbol pointing _exactly_ at this one.
813 for (sym = s->gsyms; sym; sym = sym->next)
814 if (sym->value == offset)
815 break;
816 } else {
818 * Find the nearest symbol below this one.
820 sym = NULL;
821 for (sm = s->gsyms; sm; sm = sm->next)
822 if (sm->value <= offset && (!sym || sm->value > sym->value))
823 sym = sm;
825 if (!sym && exact) {
826 error(ERR_NONFATAL, "unable to find a suitable global symbol"
827 " for this reference");
828 return 0;
831 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
832 sect->tail = &r->next;
833 r->next = NULL;
835 r->address = sect->len;
836 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
837 r->type = type;
839 sect->nrelocs++;
841 return offset - sym->value;
844 static void elf_out(int32_t segto, const void *data,
845 enum out_type type, uint64_t size,
846 int32_t segment, int32_t wrt)
848 struct Section *s;
849 int32_t addr;
850 uint8_t mydata[4], *p;
851 int i;
852 static struct symlininfo sinfo;
855 * handle absolute-assembly (structure definitions)
857 if (segto == NO_SEG) {
858 if (type != OUT_RESERVE)
859 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
860 " space");
861 return;
864 s = NULL;
865 for (i = 0; i < nsects; i++)
866 if (segto == sects[i]->index) {
867 s = sects[i];
868 break;
870 if (!s) {
871 int tempint; /* ignored */
872 if (segto != elf_section_names(".text", 2, &tempint))
873 error(ERR_PANIC, "strange segment conditions in ELF driver");
874 else {
875 s = sects[nsects - 1];
876 i = nsects - 1;
880 /* again some stabs debugging stuff */
881 if (of_elf32.current_dfmt) {
882 sinfo.offset = s->len;
883 sinfo.section = i;
884 sinfo.name = s->name;
885 of_elf32.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
887 /* end of debugging stuff */
889 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
890 error(ERR_WARNING, "attempt to initialize memory in"
891 " BSS section `%s': ignored", s->name);
892 if (type == OUT_REL2ADR)
893 size = 2;
894 else if (type == OUT_REL4ADR)
895 size = 4;
896 s->len += size;
897 return;
900 if (type == OUT_RESERVE) {
901 if (s->type == SHT_PROGBITS) {
902 error(ERR_WARNING, "uninitialized space declared in"
903 " non-BSS section `%s': zeroing", s->name);
904 elf_sect_write(s, NULL, size);
905 } else
906 s->len += size;
907 } else if (type == OUT_RAWDATA) {
908 if (segment != NO_SEG)
909 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
910 elf_sect_write(s, data, size);
911 } else if (type == OUT_ADDRESS) {
912 bool gnu16 = false;
913 addr = *(int64_t *)data;
914 if (segment != NO_SEG) {
915 if (segment % 2) {
916 error(ERR_NONFATAL, "ELF format does not support"
917 " segment base references");
918 } else {
919 if (wrt == NO_SEG) {
920 if (size == 2) {
921 gnu16 = true;
922 elf_add_reloc(s, segment, R_386_16);
923 } else {
924 elf_add_reloc(s, segment, R_386_32);
926 } else if (wrt == elf_gotpc_sect + 1) {
928 * The user will supply GOT relative to $$. ELF
929 * will let us have GOT relative to $. So we
930 * need to fix up the data item by $-$$.
932 addr += s->len;
933 elf_add_reloc(s, segment, R_386_GOTPC);
934 } else if (wrt == elf_gotoff_sect + 1) {
935 elf_add_reloc(s, segment, R_386_GOTOFF);
936 } else if (wrt == elf_got_sect + 1) {
937 addr = elf_add_gsym_reloc(s, segment, addr,
938 R_386_GOT32, true);
939 } else if (wrt == elf_sym_sect + 1) {
940 if (size == 2) {
941 gnu16 = true;
942 addr = elf_add_gsym_reloc(s, segment, addr,
943 R_386_16, false);
944 } else {
945 addr = elf_add_gsym_reloc(s, segment, addr,
946 R_386_32, false);
948 } else if (wrt == elf_plt_sect + 1) {
949 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
950 "relative PLT references");
951 } else {
952 error(ERR_NONFATAL, "ELF format does not support this"
953 " use of WRT");
954 wrt = NO_SEG; /* we can at least _try_ to continue */
958 p = mydata;
959 if (gnu16) {
960 error(ERR_WARNING | ERR_WARN_GNUELF,
961 "16-bit relocations in ELF is a GNU extension");
962 WRITESHORT(p, addr);
963 } else {
964 if (size != 4 && segment != NO_SEG) {
965 error(ERR_NONFATAL,
966 "Unsupported non-32-bit ELF relocation");
968 WRITELONG(p, addr);
970 elf_sect_write(s, mydata, size);
971 } else if (type == OUT_REL2ADR) {
972 if (segment == segto)
973 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
974 if (segment != NO_SEG && segment % 2) {
975 error(ERR_NONFATAL, "ELF format does not support"
976 " segment base references");
977 } else {
978 if (wrt == NO_SEG) {
979 error(ERR_WARNING | ERR_WARN_GNUELF,
980 "16-bit relocations in ELF is a GNU extension");
981 elf_add_reloc(s, segment, R_386_PC16);
982 } else {
983 error(ERR_NONFATAL,
984 "Unsupported non-32-bit ELF relocation");
987 p = mydata;
988 WRITESHORT(p, *(int64_t *)data - size);
989 elf_sect_write(s, mydata, 2L);
990 } else if (type == OUT_REL4ADR) {
991 if (segment == segto)
992 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
993 if (segment != NO_SEG && segment % 2) {
994 error(ERR_NONFATAL, "ELF format does not support"
995 " segment base references");
996 } else {
997 if (wrt == NO_SEG) {
998 elf_add_reloc(s, segment, R_386_PC32);
999 } else if (wrt == elf_plt_sect + 1) {
1000 elf_add_reloc(s, segment, R_386_PLT32);
1001 } else if (wrt == elf_gotpc_sect + 1 ||
1002 wrt == elf_gotoff_sect + 1 ||
1003 wrt == elf_got_sect + 1) {
1004 error(ERR_NONFATAL, "ELF format cannot produce PC-"
1005 "relative GOT references");
1006 } else {
1007 error(ERR_NONFATAL, "ELF format does not support this"
1008 " use of WRT");
1009 wrt = NO_SEG; /* we can at least _try_ to continue */
1012 p = mydata;
1013 WRITELONG(p, *(int64_t *)data - size);
1014 elf_sect_write(s, mydata, 4L);
1018 static void elf_write(void)
1020 int align;
1021 int scount;
1022 char *p;
1023 int commlen;
1024 char comment[64];
1025 int i;
1027 struct SAA *symtab;
1028 int32_t symtablen, symtablocal;
1031 * Work out how many sections we will have. We have SHN_UNDEF,
1032 * then the flexible user sections, then the four fixed
1033 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
1034 * then optionally relocation sections for the user sections.
1036 if (of_elf32.current_dfmt == &df_stabs)
1037 nsections = 8;
1038 else if (of_elf32.current_dfmt == &df_dwarf)
1039 nsections = 15;
1040 else
1041 nsections = 5; /* SHN_UNDEF and the fixed ones */
1043 add_sectname("", ".comment");
1044 add_sectname("", ".shstrtab");
1045 add_sectname("", ".symtab");
1046 add_sectname("", ".strtab");
1047 for (i = 0; i < nsects; i++) {
1048 nsections++; /* for the section itself */
1049 if (sects[i]->head) {
1050 nsections++; /* for its relocations */
1051 add_sectname(".rel", sects[i]->name);
1055 if (of_elf32.current_dfmt == &df_stabs) {
1056 /* in case the debug information is wanted, just add these three sections... */
1057 add_sectname("", ".stab");
1058 add_sectname("", ".stabstr");
1059 add_sectname(".rel", ".stab");
1062 else if (of_elf32.current_dfmt == &df_dwarf) {
1063 /* the dwarf debug standard specifies the following ten sections,
1064 not all of which are currently implemented,
1065 although all of them are defined. */
1066 #define debug_aranges (int32_t) (nsections-10)
1067 #define debug_info (int32_t) (nsections-7)
1068 #define debug_abbrev (int32_t) (nsections-5)
1069 #define debug_line (int32_t) (nsections-4)
1070 add_sectname("", ".debug_aranges");
1071 add_sectname(".rela", ".debug_aranges");
1072 add_sectname("", ".debug_pubnames");
1073 add_sectname("", ".debug_info");
1074 add_sectname(".rela", ".debug_info");
1075 add_sectname("", ".debug_abbrev");
1076 add_sectname("", ".debug_line");
1077 add_sectname(".rela", ".debug_line");
1078 add_sectname("", ".debug_frame");
1079 add_sectname("", ".debug_loc");
1083 * Do the comment.
1085 *comment = '\0';
1086 commlen =
1087 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
1090 * Output the ELF header.
1092 fwrite("\177ELF\1\1\1", 7, 1, elffp);
1093 fputc(elf_osabi, elffp);
1094 fputc(elf_abiver, elffp);
1095 fwrite("\0\0\0\0\0\0\0", 7, 1, elffp);
1096 fwriteint16_t(1, elffp); /* ET_REL relocatable file */
1097 fwriteint16_t(3, elffp); /* EM_386 processor ID */
1098 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1099 fwriteint32_t(0L, elffp); /* no entry point */
1100 fwriteint32_t(0L, elffp); /* no program header table */
1101 fwriteint32_t(0x40L, elffp); /* section headers straight after
1102 * ELF header plus alignment */
1103 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1104 fwriteint16_t(0x34, elffp); /* size of ELF header */
1105 fwriteint16_t(0, elffp); /* no program header table, again */
1106 fwriteint16_t(0, elffp); /* still no program header table */
1107 fwriteint16_t(0x28, elffp); /* size of section header */
1108 fwriteint16_t(nsections, elffp); /* number of sections */
1109 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1110 * section header table */
1111 fwriteint32_t(0L, elffp); /* align to 0x40 bytes */
1112 fwriteint32_t(0L, elffp);
1113 fwriteint32_t(0L, elffp);
1116 * Build the symbol table and relocation tables.
1118 symtab = elf_build_symtab(&symtablen, &symtablocal);
1119 for (i = 0; i < nsects; i++)
1120 if (sects[i]->head)
1121 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1122 sects[i]->head);
1125 * Now output the section header table.
1128 elf_foffs = 0x40 + 0x28 * nsections;
1129 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1130 elf_foffs += align;
1131 elf_nsect = 0;
1132 elf_sects = nasm_malloc(sizeof(*elf_sects) * nsections);
1134 elf_section_header(0, 0, 0, NULL, false, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1135 scount = 1; /* needed for the stabs debugging to track the symtable section */
1136 p = shstrtab + 1;
1137 for (i = 0; i < nsects; i++) {
1138 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1139 (sects[i]->type == SHT_PROGBITS ?
1140 sects[i]->data : NULL), true,
1141 sects[i]->len, 0, 0, sects[i]->align, 0);
1142 p += strlen(p) + 1;
1143 scount++; /* dito */
1145 elf_section_header(p - shstrtab, 1, 0, comment, false, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1146 scount++; /* dito */
1147 p += strlen(p) + 1;
1148 elf_section_header(p - shstrtab, 3, 0, shstrtab, false, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1149 scount++; /* dito */
1150 p += strlen(p) + 1;
1151 elf_section_header(p - shstrtab, 2, 0, symtab, true, symtablen, nsects + 4, symtablocal, 4, 16); /* .symtab */
1152 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1153 p += strlen(p) + 1;
1154 elf_section_header(p - shstrtab, 3, 0, strs, true, strslen, 0, 0, 1, 0); /* .strtab */
1155 for (i = 0; i < nsects; i++)
1156 if (sects[i]->head) {
1157 p += strlen(p) + 1;
1158 elf_section_header(p - shstrtab, 9, 0, sects[i]->rel, true,
1159 sects[i]->rellen, nsects + 3, i + 1, 4, 8);
1161 if (of_elf32.current_dfmt == &df_stabs) {
1162 /* for debugging information, create the last three sections
1163 which are the .stab , .stabstr and .rel.stab sections respectively */
1165 /* this function call creates the stab sections in memory */
1166 stabs32_generate();
1168 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1169 p += strlen(p) + 1;
1170 elf_section_header(p - shstrtab, 1, 0, stabbuf, false, stablen,
1171 nsections - 2, 0, 4, 12);
1173 p += strlen(p) + 1;
1174 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, false,
1175 stabstrlen, 0, 0, 4, 0);
1177 p += strlen(p) + 1;
1178 /* link -> symtable info -> section to refer to */
1179 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, false,
1180 stabrellen, symtabsection, nsections - 3, 4,
1184 else if (of_elf32.current_dfmt == &df_dwarf) {
1185 /* for dwarf debugging information, create the ten dwarf sections */
1187 /* this function call creates the dwarf sections in memory */
1188 dwarf32_generate();
1190 p += strlen(p) + 1;
1191 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, arangesbuf, false,
1192 arangeslen, 0, 0, 1, 0);
1193 p += strlen(p) + 1;
1194 elf_section_header(p - shstrtab, SHT_RELA, 0, arangesrelbuf, false,
1195 arangesrellen, symtabsection, debug_aranges, 1, 12);
1196 p += strlen(p) + 1;
1197 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, pubnamesbuf, false,
1198 pubnameslen, 0, 0, 1, 0);
1199 p += strlen(p) + 1;
1200 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, infobuf, false,
1201 infolen, 0, 0, 1, 0);
1202 p += strlen(p) + 1;
1203 elf_section_header(p - shstrtab, SHT_RELA, 0, inforelbuf, false,
1204 inforellen, symtabsection, debug_info, 1, 12);
1205 p += strlen(p) + 1;
1206 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, abbrevbuf, false,
1207 abbrevlen, 0, 0, 1, 0);
1208 p += strlen(p) + 1;
1209 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, linebuf, false,
1210 linelen, 0, 0, 1, 0);
1211 p += strlen(p) + 1;
1212 elf_section_header(p - shstrtab, SHT_RELA, 0, linerelbuf, false,
1213 linerellen, symtabsection, debug_line, 1, 12);
1214 p += strlen(p) + 1;
1215 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, framebuf, false,
1216 framelen, 0, 0, 8, 0);
1217 p += strlen(p) + 1;
1218 elf_section_header(p - shstrtab, SHT_PROGBITS, 0, locbuf, false,
1219 loclen, 0, 0, 1, 0);
1222 fwrite(align_str, align, 1, elffp);
1225 * Now output the sections.
1227 elf_write_sections();
1229 nasm_free(elf_sects);
1230 saa_free(symtab);
1233 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1235 struct SAA *s = saa_init(1L);
1236 struct Symbol *sym;
1237 uint8_t entry[16], *p;
1238 int i;
1240 *len = *local = 0;
1243 * First, an all-zeros entry, required by the ELF spec.
1245 saa_wbytes(s, NULL, 16L); /* null symbol table entry */
1246 *len += 16;
1247 (*local)++;
1250 * Next, an entry for the file name.
1252 p = entry;
1253 WRITELONG(p, 1); /* we know it's 1st entry in strtab */
1254 WRITELONG(p, 0); /* no value */
1255 WRITELONG(p, 0); /* no size either */
1256 WRITESHORT(p, STT_FILE); /* type FILE */
1257 WRITESHORT(p, SHN_ABS);
1258 saa_wbytes(s, entry, 16L);
1259 *len += 16;
1260 (*local)++;
1263 * Now some standard symbols defining the segments, for relocation
1264 * purposes.
1266 for (i = 1; i <= nsects; i++) {
1267 p = entry;
1268 WRITELONG(p, 0); /* no symbol name */
1269 WRITELONG(p, 0); /* offset zero */
1270 WRITELONG(p, 0); /* size zero */
1271 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1272 WRITESHORT(p, i); /* section id */
1273 saa_wbytes(s, entry, 16L);
1274 *len += 16;
1275 (*local)++;
1279 * Now the other local symbols.
1281 saa_rewind(syms);
1282 while ((sym = saa_rstruct(syms))) {
1283 if (sym->type & SYM_GLOBAL)
1284 continue;
1285 p = entry;
1286 WRITELONG(p, sym->strpos);
1287 WRITELONG(p, sym->value);
1288 WRITELONG(p, sym->size);
1289 WRITECHAR(p, sym->type); /* type and binding */
1290 WRITECHAR(p, sym->other); /* visibility */
1291 WRITESHORT(p, sym->section);
1292 saa_wbytes(s, entry, 16L);
1293 *len += 16;
1294 (*local)++;
1297 * dwarf needs symbols for debug sections
1298 * which are relocation targets.
1300 //*** fix for 32 bit
1301 if (of_elf32.current_dfmt == &df_dwarf) {
1302 dwarf_infosym = *local;
1303 p = entry;
1304 WRITELONG(p, 0); /* no symbol name */
1305 WRITELONG(p, (uint32_t) 0); /* offset zero */
1306 WRITELONG(p, (uint32_t) 0); /* size zero */
1307 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1308 WRITESHORT(p, debug_info); /* section id */
1309 saa_wbytes(s, entry, 16L);
1310 *len += 16;
1311 (*local)++;
1312 dwarf_abbrevsym = *local;
1313 p = entry;
1314 WRITELONG(p, 0); /* no symbol name */
1315 WRITELONG(p, (uint32_t) 0); /* offset zero */
1316 WRITELONG(p, (uint32_t) 0); /* size zero */
1317 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1318 WRITESHORT(p, debug_abbrev); /* section id */
1319 saa_wbytes(s, entry, 16L);
1320 *len += 16;
1321 (*local)++;
1322 dwarf_linesym = *local;
1323 p = entry;
1324 WRITELONG(p, 0); /* no symbol name */
1325 WRITELONG(p, (uint32_t) 0); /* offset zero */
1326 WRITELONG(p, (uint32_t) 0); /* size zero */
1327 WRITESHORT(p, STT_SECTION); /* type, binding, and visibility */
1328 WRITESHORT(p, debug_line); /* section id */
1329 saa_wbytes(s, entry, 16L);
1330 *len += 16;
1331 (*local)++;
1335 * Now the global symbols.
1337 saa_rewind(syms);
1338 while ((sym = saa_rstruct(syms))) {
1339 if (!(sym->type & SYM_GLOBAL))
1340 continue;
1341 p = entry;
1342 WRITELONG(p, sym->strpos);
1343 WRITELONG(p, sym->value);
1344 WRITELONG(p, sym->size);
1345 WRITECHAR(p, sym->type); /* type and binding */
1346 WRITECHAR(p, sym->other); /* visibility */
1347 WRITESHORT(p, sym->section);
1348 saa_wbytes(s, entry, 16L);
1349 *len += 16;
1352 return s;
1355 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1357 struct SAA *s;
1358 uint8_t *p, entry[8];
1360 if (!r)
1361 return NULL;
1363 s = saa_init(1L);
1364 *len = 0;
1366 while (r) {
1367 int32_t sym = r->symbol;
1369 if (sym >= GLOBAL_TEMP_BASE)
1371 if (of_elf32.current_dfmt == &df_dwarf)
1372 sym += -GLOBAL_TEMP_BASE + (nsects + 5) + nlocals;
1373 else sym += -GLOBAL_TEMP_BASE + (nsects + 2) + nlocals;
1376 p = entry;
1377 WRITELONG(p, r->address);
1378 WRITELONG(p, (sym << 8) + r->type);
1379 saa_wbytes(s, entry, 8L);
1380 *len += 8;
1382 r = r->next;
1385 return s;
1388 static void elf_section_header(int name, int type, int flags,
1389 void *data, bool is_saa, int32_t datalen,
1390 int link, int info, int align, int eltsize)
1392 elf_sects[elf_nsect].data = data;
1393 elf_sects[elf_nsect].len = datalen;
1394 elf_sects[elf_nsect].is_saa = is_saa;
1395 elf_nsect++;
1397 fwriteint32_t((int32_t)name, elffp);
1398 fwriteint32_t((int32_t)type, elffp);
1399 fwriteint32_t((int32_t)flags, elffp);
1400 fwriteint32_t(0L, elffp); /* no address, ever, in object files */
1401 fwriteint32_t(type == 0 ? 0L : elf_foffs, elffp);
1402 fwriteint32_t(datalen, elffp);
1403 if (data)
1404 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1405 fwriteint32_t((int32_t)link, elffp);
1406 fwriteint32_t((int32_t)info, elffp);
1407 fwriteint32_t((int32_t)align, elffp);
1408 fwriteint32_t((int32_t)eltsize, elffp);
1411 static void elf_write_sections(void)
1413 int i;
1414 for (i = 0; i < elf_nsect; i++)
1415 if (elf_sects[i].data) {
1416 int32_t len = elf_sects[i].len;
1417 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1418 int32_t align = reallen - len;
1419 if (elf_sects[i].is_saa)
1420 saa_fpwrite(elf_sects[i].data, elffp);
1421 else
1422 fwrite(elf_sects[i].data, len, 1, elffp);
1423 fwrite(align_str, align, 1, elffp);
1427 static void elf_sect_write(struct Section *sect,
1428 const uint8_t *data, uint32_t len)
1430 saa_wbytes(sect->data, data, len);
1431 sect->len += len;
1434 static int32_t elf_segbase(int32_t segment)
1436 return segment;
1439 static int elf_directive(char *directive, char *value, int pass)
1441 bool err;
1442 int64_t n;
1443 char *p;
1445 if (!strcmp(directive, "osabi")) {
1446 if (pass == 2)
1447 return 1; /* ignore in pass 2 */
1449 n = readnum(value, &err);
1450 if (err) {
1451 error(ERR_NONFATAL, "`osabi' directive requires a parameter");
1452 return 1;
1454 if (n < 0 || n > 255) {
1455 error(ERR_NONFATAL, "valid osabi numbers are 0 to 255");
1456 return 1;
1458 elf_osabi = n;
1459 elf_abiver = 0;
1461 if ((p = strchr(value,',')) == NULL)
1462 return 1;
1464 n = readnum(p+1, &err);
1465 if (err || n < 0 || n > 255) {
1466 error(ERR_NONFATAL, "invalid ABI version number (valid: 0 to 255)");
1467 return 1;
1470 elf_abiver = n;
1471 return 1;
1474 return 0;
1477 static void elf_filename(char *inname, char *outname, efunc error)
1479 strcpy(elf_module, inname);
1480 standard_extension(inname, outname, ".o", error);
1483 static const char *elf_stdmac[] = {
1484 "%define __SECT__ [section .text]",
1485 "%macro __NASM_CDecl__ 1",
1486 "%define $_%1 $%1",
1487 "%endmacro",
1488 "%macro osabi 1+.nolist",
1489 "[osabi %1]",
1490 "%endmacro",
1491 NULL
1493 static int elf_set_info(enum geninfo type, char **val)
1495 (void)type;
1496 (void)val;
1497 return 0;
1499 static struct dfmt df_dwarf = {
1500 "elf32 (X86_64) dwarf debug format for Linux",
1501 "dwarf",
1502 debug32_init,
1503 dwarf32_linenum,
1504 debug32_deflabel,
1505 debug32_directive,
1506 debug32_typevalue,
1507 dwarf32_output,
1508 dwarf32_cleanup
1510 static struct dfmt df_stabs = {
1511 "ELF32 (i386) stabs debug format for Linux",
1512 "stabs",
1513 debug32_init,
1514 stabs32_linenum,
1515 debug32_deflabel,
1516 debug32_directive,
1517 debug32_typevalue,
1518 stabs32_output,
1519 stabs32_cleanup
1522 struct dfmt *elf32_debugs_arr[3] = { &df_stabs, &df_dwarf, NULL };
1524 struct ofmt of_elf32 = {
1525 "ELF32 (i386) object files (e.g. Linux)",
1526 "elf32",
1527 NULL,
1528 elf32_debugs_arr,
1529 &null_debug_form,
1530 elf_stdmac,
1531 elf_init,
1532 elf_set_info,
1533 elf_out,
1534 elf_deflabel,
1535 elf_section_names,
1536 elf_segbase,
1537 elf_directive,
1538 elf_filename,
1539 elf_cleanup
1542 struct ofmt of_elf = {
1543 "ELF (short name for ELF32) ",
1544 "elf",
1545 NULL,
1546 elf32_debugs_arr,
1547 &null_debug_form,
1548 elf_stdmac,
1549 elf_init,
1550 elf_set_info,
1551 elf_out,
1552 elf_deflabel,
1553 elf_section_names,
1554 elf_segbase,
1555 elf_directive,
1556 elf_filename,
1557 elf_cleanup
1559 /* again, the stabs debugging stuff (code) */
1561 void debug32_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1563 (void)of;
1564 (void)id;
1565 (void)fp;
1566 (void)error;
1569 void stabs32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1571 (void)segto;
1573 if (!stabs_filename) {
1574 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1575 strcpy(stabs_filename, filename);
1576 } else {
1577 if (strcmp(stabs_filename, filename)) {
1578 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1579 in fact, this leak comes in quite handy to maintain a list of files
1580 encountered so far in the symbol lines... */
1582 /* why not nasm_free(stabs_filename); we're done with the old one */
1584 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1585 strcpy(stabs_filename, filename);
1588 debug_immcall = 1;
1589 currentline = linenumber;
1592 void debug32_deflabel(char *name, int32_t segment, int64_t offset, int is_global,
1593 char *special)
1595 (void)name;
1596 (void)segment;
1597 (void)offset;
1598 (void)is_global;
1599 (void)special;
1602 void debug32_directive(const char *directive, const char *params)
1604 (void)directive;
1605 (void)params;
1608 void debug32_typevalue(int32_t type)
1610 int32_t stype, ssize;
1611 switch (TYM_TYPE(type)) {
1612 case TY_LABEL:
1613 ssize = 0;
1614 stype = STT_NOTYPE;
1615 break;
1616 case TY_BYTE:
1617 ssize = 1;
1618 stype = STT_OBJECT;
1619 break;
1620 case TY_WORD:
1621 ssize = 2;
1622 stype = STT_OBJECT;
1623 break;
1624 case TY_DWORD:
1625 ssize = 4;
1626 stype = STT_OBJECT;
1627 break;
1628 case TY_FLOAT:
1629 ssize = 4;
1630 stype = STT_OBJECT;
1631 break;
1632 case TY_QWORD:
1633 ssize = 8;
1634 stype = STT_OBJECT;
1635 break;
1636 case TY_TBYTE:
1637 ssize = 10;
1638 stype = STT_OBJECT;
1639 break;
1640 case TY_OWORD:
1641 ssize = 8;
1642 stype = STT_OBJECT;
1643 break;
1644 case TY_COMMON:
1645 ssize = 0;
1646 stype = STT_COMMON;
1647 break;
1648 case TY_SEG:
1649 ssize = 0;
1650 stype = STT_SECTION;
1651 break;
1652 case TY_EXTERN:
1653 ssize = 0;
1654 stype = STT_NOTYPE;
1655 break;
1656 case TY_EQU:
1657 ssize = 0;
1658 stype = STT_NOTYPE;
1659 break;
1660 default:
1661 ssize = 0;
1662 stype = STT_NOTYPE;
1663 break;
1665 if (stype == STT_OBJECT && lastsym && !lastsym->type) {
1666 lastsym->size = ssize;
1667 lastsym->type = stype;
1671 void stabs32_output(int type, void *param)
1673 struct symlininfo *s;
1674 struct linelist *el;
1675 if (type == TY_STABSSYMLIN) {
1676 if (debug_immcall) {
1677 s = (struct symlininfo *)param;
1678 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1679 return; /* we are only interested in the text stuff */
1680 numlinestabs++;
1681 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1682 el->info.offset = s->offset;
1683 el->info.section = s->section;
1684 el->info.name = s->name;
1685 el->line = currentline;
1686 el->filename = stabs_filename;
1687 el->next = 0;
1688 if (stabslines) {
1689 stabslines->last->next = el;
1690 stabslines->last = el;
1691 } else {
1692 stabslines = el;
1693 stabslines->last = el;
1697 debug_immcall = 0;
1700 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1701 do {\
1702 WRITELONG(p,n_strx); \
1703 WRITECHAR(p,n_type); \
1704 WRITECHAR(p,n_other); \
1705 WRITESHORT(p,n_desc); \
1706 WRITELONG(p,n_value); \
1707 } while (0)
1709 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1711 void stabs32_generate(void)
1713 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1714 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1715 char **allfiles;
1716 int *fileidx;
1718 struct linelist *ptr;
1720 ptr = stabslines;
1722 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(char *));
1723 for (i = 0; i < numlinestabs; i++)
1724 allfiles[i] = 0;
1725 numfiles = 0;
1726 while (ptr) {
1727 if (numfiles == 0) {
1728 allfiles[0] = ptr->filename;
1729 numfiles++;
1730 } else {
1731 for (i = 0; i < numfiles; i++) {
1732 if (!strcmp(allfiles[i], ptr->filename))
1733 break;
1735 if (i >= numfiles) {
1736 allfiles[i] = ptr->filename;
1737 numfiles++;
1740 ptr = ptr->next;
1742 strsize = 1;
1743 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1744 for (i = 0; i < numfiles; i++) {
1745 fileidx[i] = strsize;
1746 strsize += strlen(allfiles[i]) + 1;
1748 mainfileindex = 0;
1749 for (i = 0; i < numfiles; i++) {
1750 if (!strcmp(allfiles[i], elf_module)) {
1751 mainfileindex = i;
1752 break;
1756 /* worst case size of the stab buffer would be:
1757 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1759 sbuf =
1760 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1761 sizeof(struct stabentry));
1763 ssbuf = (uint8_t *)nasm_malloc(strsize);
1765 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 8 * (2 + 3));
1766 rptr = rbuf;
1768 for (i = 0; i < numfiles; i++) {
1769 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1771 ssbuf[0] = 0;
1773 stabstrlen = strsize; /* set global variable for length of stab strings */
1775 sptr = sbuf;
1776 ptr = stabslines;
1777 numstabs = 0;
1779 if (ptr) {
1780 /* this is the first stab, its strx points to the filename of the
1781 the source-file, the n_desc field should be set to the number
1782 of remaining stabs
1784 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1786 /* this is the stab for the main source file */
1787 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1789 /* relocation table entry */
1791 /* Since the symbol table has two entries before */
1792 /* the section symbols, the index in the info.section */
1793 /* member must be adjusted by adding 2 */
1795 WRITELONG(rptr, (sptr - sbuf) - 4);
1796 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1798 numstabs++;
1799 currfile = mainfileindex;
1802 while (ptr) {
1803 if (strcmp(allfiles[currfile], ptr->filename)) {
1804 /* oops file has changed... */
1805 for (i = 0; i < numfiles; i++)
1806 if (!strcmp(allfiles[i], ptr->filename))
1807 break;
1808 currfile = i;
1809 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1810 ptr->info.offset);
1811 numstabs++;
1813 /* relocation table entry */
1814 WRITELONG(rptr, (sptr - sbuf) - 4);
1815 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1818 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1819 numstabs++;
1821 /* relocation table entry */
1823 WRITELONG(rptr, (sptr - sbuf) - 4);
1824 WRITELONG(rptr, ((ptr->info.section + 2) << 8) | R_386_32);
1826 ptr = ptr->next;
1830 ((struct stabentry *)sbuf)->n_desc = numstabs;
1832 nasm_free(allfiles);
1833 nasm_free(fileidx);
1835 stablen = (sptr - sbuf);
1836 stabrellen = (rptr - rbuf);
1837 stabrelbuf = rbuf;
1838 stabbuf = sbuf;
1839 stabstrbuf = ssbuf;
1842 void stabs32_cleanup(void)
1844 struct linelist *ptr, *del;
1845 if (!stabslines)
1846 return;
1847 ptr = stabslines;
1848 while (ptr) {
1849 del = ptr;
1850 ptr = ptr->next;
1851 nasm_free(del);
1853 if (stabbuf)
1854 nasm_free(stabbuf);
1855 if (stabrelbuf)
1856 nasm_free(stabrelbuf);
1857 if (stabstrbuf)
1858 nasm_free(stabstrbuf);
1860 /* dwarf routines */
1863 void dwarf32_linenum(const char *filename, int32_t linenumber, int32_t segto)
1865 (void)segto;
1866 dwarf32_findfile(filename);
1867 debug_immcall = 1;
1868 currentline = linenumber;
1871 /* called from elf_out with type == TY_DEBUGSYMLIN */
1872 void dwarf32_output(int type, void *param)
1874 int ln, aa, inx, maxln, soc;
1875 struct symlininfo *s;
1876 struct SAA *plinep;
1878 (void)type;
1880 s = (struct symlininfo *)param;
1881 /* line number info is only gathered for executable sections */
1882 if (!(sects[s->section]->flags & SHF_EXECINSTR))
1883 return;
1884 /* Check if section index has changed */
1885 if (!(dwarf_csect && (dwarf_csect->section) == (s->section)))
1887 dwarf32_findsect(s->section);
1889 /* do nothing unless line or file has changed */
1890 if (debug_immcall)
1892 ln = currentline - dwarf_csect->line;
1893 aa = s->offset - dwarf_csect->offset;
1894 inx = dwarf_clist->line;
1895 plinep = dwarf_csect->psaa;
1896 /* check for file change */
1897 if (!(inx == dwarf_csect->file))
1899 WSAACHAR(plinep,workbuf,DW_LNS_set_file);
1900 WSAACHAR(plinep,workbuf,inx);
1901 dwarf_csect->file = inx;
1903 /* check for line change */
1904 if (ln)
1906 /* test if in range of special op code */
1907 maxln = line_base + line_range;
1908 soc = (ln - line_base) + (line_range * aa) + opcode_base;
1909 if (ln >= line_base && ln < maxln && soc < 256)
1911 WSAACHAR(plinep,workbuf,soc);
1913 else
1915 if (ln)
1917 WSAACHAR(plinep,workbuf,DW_LNS_advance_line);
1918 saa_wleb128s(plinep,ln);
1920 if (aa)
1922 WSAACHAR(plinep,workbuf,DW_LNS_advance_pc);
1923 saa_wleb128u(plinep,aa);
1926 dwarf_csect->line = currentline;
1927 dwarf_csect->offset = s->offset;
1929 /* show change handled */
1930 debug_immcall = 0;
1935 void dwarf32_generate(void)
1937 uint8_t *pbuf;
1938 int indx;
1939 struct linelist *ftentry;
1940 struct SAA *paranges, *ppubnames, *pinfo, *pabbrev, *plines, *plinep;
1941 struct SAA *parangesrel, *plinesrel, *pinforel;
1942 struct sectlist *psect;
1943 size_t saalen, linepoff, totlen, highaddr;
1945 /* write epilogues for each line program range */
1946 /* and build aranges section */
1947 paranges = saa_init(1L);
1948 parangesrel = saa_init(1L);
1949 WSAASHORT(paranges,workbuf,2); /* dwarf version */
1950 WSAALONG(parangesrel,workbuf, paranges->datalen+4);
1951 WSAALONG(parangesrel,workbuf, (dwarf_infosym << 8) + R_386_32); /* reloc to info */
1952 WSAALONG(parangesrel,workbuf, (uint32_t) 0);
1953 WSAALONG(paranges,workbuf,0); /* offset into info */
1954 WSAACHAR(paranges,workbuf,4); /* pointer size */
1955 WSAACHAR(paranges,workbuf,0); /* not segmented */
1956 WSAALONG(paranges,workbuf,0); /* padding */
1957 /* iterate though sectlist entries */
1958 psect = dwarf_fsect;
1959 totlen = 0;
1960 highaddr = 0;
1961 for (indx = 0; indx < dwarf_nsections; indx++)
1963 plinep = psect->psaa;
1964 /* Line Number Program Epilogue */
1965 WSAACHAR(plinep,workbuf,2); /* std op 2 */
1966 WSAACHAR(plinep,workbuf,(sects[psect->section]->len)-psect->offset);
1967 WSAACHAR(plinep,workbuf,DW_LNS_extended_op);
1968 WSAACHAR(plinep,workbuf,1); /* operand length */
1969 WSAACHAR(plinep,workbuf,DW_LNE_end_sequence);
1970 totlen += plinep->datalen;
1971 /* range table relocation entry */
1972 WSAALONG(parangesrel,workbuf, paranges->datalen + 4);
1973 WSAALONG(parangesrel,workbuf, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
1974 WSAALONG(parangesrel,workbuf, (uint32_t) 0);
1975 /* range table entry */
1976 WSAALONG(paranges,workbuf,0x0000); /* range start */
1977 WSAALONG(paranges,workbuf,sects[psect->section]->len); /* range length */
1978 highaddr += sects[psect->section]->len;
1979 /* done with this entry */
1980 psect = psect->next;
1982 WSAALONG(paranges,workbuf,0); /* null address */
1983 WSAALONG(paranges,workbuf,0); /* null length */
1984 saalen = paranges->datalen;
1985 arangeslen = saalen + 4;
1986 arangesbuf = pbuf = nasm_malloc(arangeslen);
1987 WRITELONG(pbuf,saalen); /* initial length */
1988 saa_rnbytes(paranges, pbuf, saalen);
1989 saa_free(paranges);
1991 /* build rela.aranges section */
1992 arangesrellen = saalen = parangesrel->datalen;
1993 arangesrelbuf = pbuf = nasm_malloc(arangesrellen);
1994 saa_rnbytes(parangesrel, pbuf, saalen);
1995 saa_free(parangesrel);
1997 /* build pubnames section */
1998 ppubnames = saa_init(1L);
1999 WSAASHORT(ppubnames,workbuf,3); /* dwarf version */
2000 WSAALONG(ppubnames,workbuf,0); /* offset into info */
2001 WSAALONG(ppubnames,workbuf,0); /* space used in info */
2002 WSAALONG(ppubnames,workbuf,0); /* end of list */
2003 saalen = ppubnames->datalen;
2004 pubnameslen = saalen + 4;
2005 pubnamesbuf = pbuf = nasm_malloc(pubnameslen);
2006 WRITELONG(pbuf,saalen); /* initial length */
2007 saa_rnbytes(ppubnames, pbuf, saalen);
2008 saa_free(ppubnames);
2010 /* build info section */
2011 pinfo = saa_init(1L);
2012 pinforel = saa_init(1L);
2013 WSAASHORT(pinfo,workbuf,2); /* dwarf version */
2014 WSAALONG(pinforel,workbuf, pinfo->datalen + 4);
2015 WSAALONG(pinforel,workbuf, (dwarf_abbrevsym << 8) + R_386_32); /* reloc to abbrev */
2016 WSAALONG(pinforel,workbuf, (uint32_t) 0);
2017 WSAALONG(pinfo,workbuf,0); /* offset into abbrev */
2018 WSAACHAR(pinfo,workbuf,4); /* pointer size */
2019 WSAACHAR(pinfo,workbuf,1); /* abbrviation number LEB128u */
2020 WSAALONG(pinforel,workbuf, pinfo->datalen + 4);
2021 WSAALONG(pinforel,workbuf, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2022 WSAALONG(pinforel,workbuf, (uint32_t) 0);
2023 WSAALONG(pinfo,workbuf,0); /* DW_AT_low_pc */
2024 WSAALONG(pinforel,workbuf, pinfo->datalen + 4);
2025 WSAALONG(pinforel,workbuf, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2026 WSAALONG(pinforel,workbuf, (uint32_t) 0);
2027 WSAALONG(pinfo,workbuf,highaddr); /* DW_AT_high_pc */
2028 WSAALONG(pinforel,workbuf, pinfo->datalen + 4);
2029 WSAALONG(pinforel,workbuf, (dwarf_linesym << 8) + R_386_32); /* reloc to line */
2030 WSAALONG(pinforel,workbuf, (uint32_t) 0);
2031 WSAALONG(pinfo,workbuf,0); /* DW_AT_stmt_list */
2032 strcpy(workbuf,elf_module); /* input file name */
2033 saa_wbytes(pinfo, workbuf, (int32_t)(strlen(elf_module) + 1));
2034 sprintf(workbuf, "NASM %s", NASM_VER);
2035 saa_wbytes(pinfo, workbuf, (int32_t)(strlen(workbuf) + 1));
2036 WSAASHORT(pinfo,workbuf,DW_LANG_Mips_Assembler);
2037 WSAACHAR(pinfo,workbuf,2); /* abbrviation number LEB128u */
2038 WSAALONG(pinforel,workbuf, pinfo->datalen + 4);
2039 WSAALONG(pinforel,workbuf, ((dwarf_fsect->section + 2) << 8) + R_386_32);
2040 WSAALONG(pinforel,workbuf, (uint32_t) 0);
2041 WSAALONG(pinfo,workbuf,0); /* DW_AT_low_pc */
2042 WSAALONG(pinfo,workbuf,0); /* DW_AT_frame_base */
2043 WSAACHAR(pinfo,workbuf,0); /* end of entries */
2044 saalen = pinfo->datalen;
2045 infolen = saalen + 4;
2046 infobuf = pbuf = nasm_malloc(infolen);
2047 WRITELONG(pbuf,saalen); /* initial length */
2048 saa_rnbytes(pinfo, pbuf, saalen);
2049 saa_free(pinfo);
2051 /* build rela.info section */
2052 inforellen = saalen = pinforel->datalen;
2053 inforelbuf = pbuf = nasm_malloc(inforellen);
2054 saa_rnbytes(pinforel, pbuf, saalen);
2055 saa_free(pinforel);
2057 /* build abbrev section */
2058 pabbrev = saa_init(1L);
2059 WSAACHAR(pabbrev,workbuf,1); /* entry number LEB128u */
2060 WSAACHAR(pabbrev,workbuf,DW_TAG_compile_unit); /* tag LEB128u */
2061 WSAACHAR(pabbrev,workbuf,1); /* has children */
2062 /* the following attributes and forms are all LEB128u values */
2063 WSAACHAR(pabbrev,workbuf,DW_AT_low_pc);
2064 WSAACHAR(pabbrev,workbuf,DW_FORM_addr);
2065 WSAACHAR(pabbrev,workbuf,DW_AT_high_pc);
2066 WSAACHAR(pabbrev,workbuf,DW_FORM_addr);
2067 WSAACHAR(pabbrev,workbuf,DW_AT_stmt_list);
2068 WSAACHAR(pabbrev,workbuf,DW_FORM_data4);
2069 WSAACHAR(pabbrev,workbuf,DW_AT_name);
2070 WSAACHAR(pabbrev,workbuf,DW_FORM_string);
2071 WSAACHAR(pabbrev,workbuf,DW_AT_producer);
2072 WSAACHAR(pabbrev,workbuf,DW_FORM_string);
2073 WSAACHAR(pabbrev,workbuf,DW_AT_language);
2074 WSAACHAR(pabbrev,workbuf,DW_FORM_data2);
2075 WSAASHORT(pabbrev,workbuf,0); /* end of entry */
2076 /* LEB128u usage same as above */
2077 WSAACHAR(pabbrev,workbuf,2); /* entry number */
2078 WSAACHAR(pabbrev,workbuf,DW_TAG_subprogram);
2079 WSAACHAR(pabbrev,workbuf,0); /* no children */
2080 WSAACHAR(pabbrev,workbuf,DW_AT_low_pc);
2081 WSAACHAR(pabbrev,workbuf,DW_FORM_addr);
2082 WSAACHAR(pabbrev,workbuf,DW_AT_frame_base);
2083 WSAACHAR(pabbrev,workbuf,DW_FORM_data4);
2084 WSAASHORT(pabbrev,workbuf,0); /* end of entry */
2085 abbrevlen = saalen = pabbrev->datalen;
2086 abbrevbuf = pbuf = nasm_malloc(saalen);
2087 saa_rnbytes(pabbrev, pbuf, saalen);
2088 saa_free(pabbrev);
2090 /* build line section */
2091 /* prolog */
2092 plines = saa_init(1L);
2093 WSAACHAR(plines,workbuf,1); /* Minimum Instruction Length */
2094 WSAACHAR(plines,workbuf,1); /* Initial value of 'is_stmt' */
2095 WSAACHAR(plines,workbuf,line_base); /* Line Base */
2096 WSAACHAR(plines,workbuf,line_range); /* Line Range */
2097 WSAACHAR(plines,workbuf,opcode_base); /* Opcode Base */
2098 /* standard opcode lengths (# of LEB128u operands) */
2099 WSAACHAR(plines,workbuf,0); /* Std opcode 1 length */
2100 WSAACHAR(plines,workbuf,1); /* Std opcode 2 length */
2101 WSAACHAR(plines,workbuf,1); /* Std opcode 3 length */
2102 WSAACHAR(plines,workbuf,1); /* Std opcode 4 length */
2103 WSAACHAR(plines,workbuf,1); /* Std opcode 5 length */
2104 WSAACHAR(plines,workbuf,0); /* Std opcode 6 length */
2105 WSAACHAR(plines,workbuf,0); /* Std opcode 7 length */
2106 WSAACHAR(plines,workbuf,0); /* Std opcode 8 length */
2107 WSAACHAR(plines,workbuf,1); /* Std opcode 9 length */
2108 WSAACHAR(plines,workbuf,0); /* Std opcode 10 length */
2109 WSAACHAR(plines,workbuf,0); /* Std opcode 11 length */
2110 WSAACHAR(plines,workbuf,1); /* Std opcode 12 length */
2111 /* Directory Table */
2112 WSAACHAR(plines,workbuf,0); /* End of table */
2113 /* File Name Table */
2114 ftentry = dwarf_flist;
2115 for (indx = 0;indx<dwarf_numfiles;indx++)
2117 saa_wbytes(plines, ftentry->filename, (int32_t)(strlen(ftentry->filename) + 1));
2118 WSAACHAR(plines,workbuf,0); /* directory LEB128u */
2119 WSAACHAR(plines,workbuf,0); /* time LEB128u */
2120 WSAACHAR(plines,workbuf,0); /* size LEB128u */
2121 ftentry = ftentry->next;
2123 WSAACHAR(plines,workbuf,0); /* End of table */
2124 linepoff = plines->datalen;
2125 linelen = linepoff + totlen + 10;
2126 linebuf = pbuf = nasm_malloc(linelen);
2127 WRITELONG(pbuf,linelen-4); /* initial length */
2128 WRITESHORT(pbuf,3); /* dwarf version */
2129 WRITELONG(pbuf,linepoff); /* offset to line number program */
2130 /* write line header */
2131 saalen = linepoff;
2132 saa_rnbytes(plines, pbuf, saalen); /* read a given no. of bytes */
2133 pbuf += linepoff;
2134 saa_free(plines);
2135 /* concatonate line program ranges */
2136 linepoff += 13;
2137 plinesrel = saa_init(1L);
2138 psect = dwarf_fsect;
2139 for (indx = 0; indx < dwarf_nsections; indx++)
2141 WSAALONG(plinesrel,workbuf, linepoff);
2142 WSAALONG(plinesrel,workbuf, ((uint32_t) (psect->section + 2) << 8) + R_386_32);
2143 WSAALONG(plinesrel,workbuf, (uint32_t) 0);
2144 plinep = psect->psaa;
2145 saalen = plinep->datalen;
2146 saa_rnbytes(plinep, pbuf, saalen);
2147 pbuf += saalen;
2148 linepoff += saalen;
2149 saa_free(plinep);
2150 /* done with this entry */
2151 psect = psect->next;
2155 /* build rela.lines section */
2156 linerellen =saalen = plinesrel->datalen;
2157 linerelbuf = pbuf = nasm_malloc(linerellen);
2158 saa_rnbytes(plinesrel, pbuf, saalen);
2159 saa_free(plinesrel);
2161 /* build frame section */
2162 framelen = 4;
2163 framebuf = pbuf = nasm_malloc(framelen);
2164 WRITELONG(pbuf,framelen-4); /* initial length */
2166 /* build loc section */
2167 loclen = 16;
2168 locbuf = pbuf = nasm_malloc(loclen);
2169 WRITELONG(pbuf,0); /* null beginning offset */
2170 WRITELONG(pbuf,0); /* null ending offset */
2173 void dwarf32_cleanup(void)
2175 if (arangesbuf)
2176 nasm_free(arangesbuf);
2177 if (arangesrelbuf)
2178 nasm_free(arangesrelbuf);
2179 if (pubnamesbuf)
2180 nasm_free(pubnamesbuf);
2181 if (infobuf)
2182 nasm_free(infobuf);
2183 if (inforelbuf)
2184 nasm_free(inforelbuf);
2185 if (abbrevbuf)
2186 nasm_free(abbrevbuf);
2187 if (linebuf)
2188 nasm_free(linebuf);
2189 if (linerelbuf)
2190 nasm_free(linerelbuf);
2191 if (framebuf)
2192 nasm_free(framebuf);
2193 if (locbuf)
2194 nasm_free(locbuf);
2196 void dwarf32_findfile(const char * fname)
2198 int finx;
2199 struct linelist *match;
2201 /* return if fname is current file name */
2202 if (dwarf_clist && !(strcmp(fname, dwarf_clist->filename))) return;
2203 /* search for match */
2204 else
2206 match = 0;
2207 if (dwarf_flist)
2209 match = dwarf_flist;
2210 for (finx = 0; finx < dwarf_numfiles; finx++)
2212 if (!(strcmp(fname, match->filename)))
2214 dwarf_clist = match;
2215 return;
2219 /* add file name to end of list */
2220 dwarf_clist = (struct linelist *)nasm_malloc(sizeof(struct linelist));
2221 dwarf_numfiles++;
2222 dwarf_clist->line = dwarf_numfiles;
2223 dwarf_clist->filename = nasm_malloc(strlen(fname) + 1);
2224 strcpy(dwarf_clist->filename,fname);
2225 dwarf_clist->next = 0;
2226 /* if first entry */
2227 if (!dwarf_flist)
2229 dwarf_flist = dwarf_elist = dwarf_clist;
2230 dwarf_clist->last = 0;
2232 /* chain to previous entry */
2233 else
2235 dwarf_elist->next = dwarf_clist;
2236 dwarf_elist = dwarf_clist;
2240 /* */
2241 void dwarf32_findsect(const int index)
2243 int sinx;
2244 struct sectlist *match;
2245 struct SAA *plinep;
2246 /* return if index is current section index */
2247 if (dwarf_csect && (dwarf_csect->section == index))
2249 return;
2251 /* search for match */
2252 else
2254 match = 0;
2255 if (dwarf_fsect)
2257 match = dwarf_fsect;
2258 for (sinx = 0; sinx < dwarf_nsections; sinx++)
2260 if ((match->section == index))
2262 dwarf_csect = match;
2263 return;
2265 match = match->next;
2268 /* add entry to end of list */
2269 dwarf_csect = (struct sectlist *)nasm_malloc(sizeof(struct sectlist));
2270 dwarf_nsections++;
2271 dwarf_csect->psaa = plinep = saa_init(1L);
2272 dwarf_csect->line = 1;
2273 dwarf_csect->offset = 0;
2274 dwarf_csect->file = 1;
2275 dwarf_csect->section = index;
2276 dwarf_csect->next = 0;
2277 /* set relocatable address at start of line program */
2278 WSAACHAR(plinep,workbuf,DW_LNS_extended_op);
2279 WSAACHAR(plinep,workbuf,5); /* operand length */
2280 WSAACHAR(plinep,workbuf,DW_LNE_set_address);
2281 WSAALONG(plinep,workbuf,0); /* Start Address */
2282 /* if first entry */
2283 if (!dwarf_fsect)
2285 dwarf_fsect = dwarf_esect = dwarf_csect;
2286 dwarf_csect->last = 0;
2288 /* chain to previous entry */
2289 else
2291 dwarf_esect->next = dwarf_csect;
2292 dwarf_esect = dwarf_csect;
2297 #endif /* OF_ELF */