attempt to make static makefiles aware of outelf32/outelf64
[nasm/nasm.git] / output / outelf64.c
blobb83bab7756e007684704b9836f076bb7bba16b22
1 /* outelf.c output routines for the Netwide Assembler to produce
2 * ELF64 (x86_64 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 <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <inttypes.h>
16 #include "nasm.h"
17 #include "nasmlib.h"
18 #include "outform.h"
20 /* Definitions in lieu of elf.h */
22 #define SHT_PROGBITS 1
23 #define SHT_RELA 4 /* Relocation entries with addends */
24 #define SHT_NOBITS 8
25 #define SHF_WRITE (1 << 0) /* Writable */
26 #define SHF_ALLOC (1 << 1) /* Occupies memory during execution */
27 #define SHF_EXECINSTR (1 << 2) /* Executable */
28 #define SHN_ABS 0xfff1 /* Associated symbol is absolute */
29 #define SHN_COMMON 0xfff2 /* Associated symbol is common */
30 #define R_X86_64_NONE 0 /* No reloc */
31 #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
32 #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
33 #define R_X86_64_PLT32 4 /* 32 bit PLT address */
34 #define R_X86_64_GOTPCREL 9 /* 32 bit signed PC relative */
35 #define R_X86_64_32 10 /* Direct 32 bit zero extended */
36 #define R_X86_64_16 12 /* Direct 16 bit zero extended */
37 #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
38 #define R_X86_64_GOTTPOFF 22 /* 32 bit signed PC relative offset */
39 #define ET_REL 1 /* Relocatable file */
40 #define EM_X86_64 62 /* AMD x86-64 architecture */
41 typedef uint32_t Elf64_Word;
42 typedef uint64_t Elf64_Xword;
43 typedef uint64_t Elf64_Addr;
44 typedef uint64_t Elf64_Off;
45 typedef struct
47 Elf64_Word sh_name; /* Section name (string tbl index) */
48 Elf64_Word sh_type; /* Section type */
49 Elf64_Xword sh_flags; /* Section flags */
50 Elf64_Addr sh_addr; /* Section virtual addr at execution */
51 Elf64_Off sh_offset; /* Section file offset */
52 Elf64_Xword sh_size; /* Section size in bytes */
53 Elf64_Word sh_link; /* Link to another section */
54 Elf64_Word sh_info; /* Additional section information */
55 Elf64_Xword sh_addralign; /* Section alignment */
56 Elf64_Xword sh_entsize; /* Entry size if section holds table */
57 } Elf64_Shdr;
60 #ifdef OF_ELF64
63 struct Reloc {
64 struct Reloc *next;
65 int64_t address; /* relative to _start_ of section */
66 int64_t symbol; /* ELF symbol info thingy */
67 int type; /* type of relocation */
70 struct Symbol {
71 int32_t strpos; /* string table position of name */
72 int32_t section; /* section ID of the symbol */
73 int type; /* symbol type */
74 int other; /* symbol visibility */
75 int32_t value; /* address, or COMMON variable align */
76 int32_t size; /* size of symbol */
77 int32_t globnum; /* symbol table offset if global */
78 struct Symbol *next; /* list of globals in each section */
79 struct Symbol *nextfwd; /* list of unresolved-size symbols */
80 char *name; /* used temporarily if in above list */
84 struct Section {
85 struct SAA *data;
86 uint32_t len, size, nrelocs;
87 int32_t index;
88 int type; /* SHT_PROGBITS or SHT_NOBITS */
89 int align; /* alignment: power of two */
90 uint32_t flags; /* section flags */
91 char *name;
92 struct SAA *rel;
93 int32_t rellen;
94 struct Reloc *head, **tail;
95 struct Symbol *gsyms; /* global symbols in section */
98 #define SECT_DELTA 32
99 static struct Section **sects;
100 static int nsects, sectlen;
102 #define SHSTR_DELTA 256
103 static char *shstrtab;
104 static int shstrtablen, shstrtabsize;
106 static struct SAA *syms;
107 static uint32_t nlocals, nglobs;
109 static int32_t def_seg;
111 static struct RAA *bsym;
113 static struct SAA *strs;
114 static uint32_t strslen;
116 static FILE *elffp;
117 static efunc error;
118 static evalfunc evaluate;
120 static struct Symbol *fwds;
122 static char elf_module[FILENAME_MAX];
124 extern struct ofmt of_elf64;
126 #define SHN_UNDEF 0
128 #define SYM_SECTION 0x04
129 #define SYM_GLOBAL 0x10
130 #define SYM_NOTYPE 0x00
131 #define SYM_DATA 0x01
132 #define SYM_FUNCTION 0x02
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 int 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 *, int, 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;
210 void stabs64_init(struct ofmt *, void *, FILE *, efunc);
211 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t);
212 void stabs64_deflabel(char *, int32_t, int32_t, int, char *);
213 void stabs64_directive(const char *, const char *);
214 void stabs64_typevalue(int32_t);
215 void stabs64_output(int, void *);
216 void stabs64_generate();
217 void stabs64_cleanup();
219 /* end of stabs debugging stuff */
222 * Special section numbers which are used to define ELF special
223 * symbols, which can be used with WRT to provide PIC relocation
224 * types.
226 static int32_t elf_gotpc_sect, elf_gotoff_sect;
227 static int32_t elf_got_sect, elf_plt_sect;
228 static int32_t elf_sym_sect;
230 static void elf_init(FILE * fp, efunc errfunc, ldfunc ldef, evalfunc eval)
232 maxbits = 64;
233 elffp = fp;
234 error = errfunc;
235 evaluate = eval;
236 (void)ldef; /* placate optimisers */
237 sects = NULL;
238 nsects = sectlen = 0;
239 syms = saa_init((int32_t)sizeof(struct Symbol));
240 nlocals = nglobs = 0;
241 bsym = raa_init();
242 strs = saa_init(1L);
243 saa_wbytes(strs, "\0", 1L);
244 saa_wbytes(strs, elf_module, (int32_t)(strlen(elf_module) + 1));
245 strslen = 2 + strlen(elf_module);
246 shstrtab = NULL;
247 shstrtablen = shstrtabsize = 0;;
248 add_sectname("", "");
250 fwds = NULL;
252 elf_gotpc_sect = seg_alloc();
253 ldef("..gotpc", elf_gotpc_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf64,
254 error);
255 elf_gotoff_sect = seg_alloc();
256 ldef("..gotoff", elf_gotoff_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf64,
257 error);
258 elf_got_sect = seg_alloc();
259 ldef("..got", elf_got_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf64,
260 error);
261 elf_plt_sect = seg_alloc();
262 ldef("..plt", elf_plt_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf64,
263 error);
264 elf_sym_sect = seg_alloc();
265 ldef("..sym", elf_sym_sect + 1, 0L, NULL, FALSE, FALSE, &of_elf64,
266 error);
268 def_seg = seg_alloc();
271 static void elf_cleanup(int debuginfo)
273 struct Reloc *r;
274 int i;
276 (void)debuginfo;
278 elf_write();
279 fclose(elffp);
280 for (i = 0; i < nsects; i++) {
281 if (sects[i]->type != SHT_NOBITS)
282 saa_free(sects[i]->data);
283 if (sects[i]->head)
284 saa_free(sects[i]->rel);
285 while (sects[i]->head) {
286 r = sects[i]->head;
287 sects[i]->head = sects[i]->head->next;
288 nasm_free(r);
291 nasm_free(sects);
292 saa_free(syms);
293 raa_free(bsym);
294 saa_free(strs);
295 if (of_elf64.current_dfmt) {
296 of_elf64.current_dfmt->cleanup();
300 static void add_sectname(char *firsthalf, char *secondhalf)
302 int len = strlen(firsthalf) + strlen(secondhalf);
303 while (shstrtablen + len + 1 > shstrtabsize)
304 shstrtab = nasm_realloc(shstrtab, (shstrtabsize += SHSTR_DELTA));
305 strcpy(shstrtab + shstrtablen, firsthalf);
306 strcat(shstrtab + shstrtablen, secondhalf);
307 shstrtablen += len + 1;
310 static int elf_make_section(char *name, int type, int flags, int align)
312 struct Section *s;
314 s = nasm_malloc(sizeof(*s));
316 if (type != SHT_NOBITS)
317 s->data = saa_init(1L);
318 s->head = NULL;
319 s->tail = &s->head;
320 s->len = s->size = 0;
321 s->nrelocs = 0;
322 if (!strcmp(name, ".text"))
323 s->index = def_seg;
324 else
325 s->index = seg_alloc();
326 add_sectname("", name);
327 s->name = nasm_malloc(1 + strlen(name));
328 strcpy(s->name, name);
329 s->type = type;
330 s->flags = flags;
331 s->align = align;
332 s->gsyms = NULL;
334 if (nsects >= sectlen)
335 sects =
336 nasm_realloc(sects, (sectlen += SECT_DELTA) * sizeof(*sects));
337 sects[nsects++] = s;
339 return nsects - 1;
342 static int32_t elf_section_names(char *name, int pass, int *bits)
344 char *p;
345 int flags_and, flags_or, type, align, i;
348 * Default is 64 bits.
350 if (!name) {
351 *bits = 64;
352 return def_seg;
355 p = name;
356 while (*p && !isspace(*p))
357 p++;
358 if (*p)
359 *p++ = '\0';
360 flags_and = flags_or = type = align = 0;
362 while (*p && isspace(*p))
363 p++;
364 while (*p) {
365 char *q = p;
366 while (*p && !isspace(*p))
367 p++;
368 if (*p)
369 *p++ = '\0';
370 while (*p && isspace(*p))
371 p++;
373 if (!nasm_strnicmp(q, "align=", 6)) {
374 align = atoi(q + 6);
375 if (align == 0)
376 align = 1;
377 if ((align - 1) & align) { /* means it's not a power of two */
378 error(ERR_NONFATAL, "section alignment %d is not"
379 " a power of two", align);
380 align = 1;
382 } else if (!nasm_stricmp(q, "alloc")) {
383 flags_and |= SHF_ALLOC;
384 flags_or |= SHF_ALLOC;
385 } else if (!nasm_stricmp(q, "noalloc")) {
386 flags_and |= SHF_ALLOC;
387 flags_or &= ~SHF_ALLOC;
388 } else if (!nasm_stricmp(q, "exec")) {
389 flags_and |= SHF_EXECINSTR;
390 flags_or |= SHF_EXECINSTR;
391 } else if (!nasm_stricmp(q, "noexec")) {
392 flags_and |= SHF_EXECINSTR;
393 flags_or &= ~SHF_EXECINSTR;
394 } else if (!nasm_stricmp(q, "write")) {
395 flags_and |= SHF_WRITE;
396 flags_or |= SHF_WRITE;
397 } else if (!nasm_stricmp(q, "nowrite")) {
398 flags_and |= SHF_WRITE;
399 flags_or &= ~SHF_WRITE;
400 } else if (!nasm_stricmp(q, "progbits")) {
401 type = SHT_PROGBITS;
402 } else if (!nasm_stricmp(q, "nobits")) {
403 type = SHT_NOBITS;
407 if (!strcmp(name, ".comment") ||
408 !strcmp(name, ".shstrtab") ||
409 !strcmp(name, ".symtab") || !strcmp(name, ".strtab")) {
410 error(ERR_NONFATAL, "attempt to redefine reserved section"
411 "name `%s'", name);
412 return NO_SEG;
415 for (i = 0; i < nsects; i++)
416 if (!strcmp(name, sects[i]->name))
417 break;
418 if (i == nsects) {
419 if (!strcmp(name, ".text"))
420 i = elf_make_section(name, SHT_PROGBITS,
421 SHF_ALLOC | SHF_EXECINSTR, 16);
422 else if (!strcmp(name, ".rodata"))
423 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 4);
424 else if (!strcmp(name, ".data"))
425 i = elf_make_section(name, SHT_PROGBITS,
426 SHF_ALLOC | SHF_WRITE, 4);
427 else if (!strcmp(name, ".bss"))
428 i = elf_make_section(name, SHT_NOBITS,
429 SHF_ALLOC | SHF_WRITE, 4);
430 else
431 i = elf_make_section(name, SHT_PROGBITS, SHF_ALLOC, 1);
432 if (type)
433 sects[i]->type = type;
434 if (align)
435 sects[i]->align = align;
436 sects[i]->flags &= ~flags_and;
437 sects[i]->flags |= flags_or;
438 } else if (pass == 1) {
439 if (type || align || flags_and)
440 error(ERR_WARNING, "section attributes ignored on"
441 " redeclaration of section `%s'", name);
444 return sects[i]->index;
447 static void elf_deflabel(char *name, int32_t segment, int32_t offset,
448 int is_global, char *special)
450 int pos = strslen;
451 struct Symbol *sym;
452 int special_used = FALSE;
454 #if defined(DEBUG) && DEBUG>2
455 fprintf(stderr,
456 " elf_deflabel: %s, seg=%x, off=%x, is_global=%d, %s\n",
457 name, segment, offset, is_global, special);
458 #endif
459 if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
461 * This is a NASM special symbol. We never allow it into
462 * the ELF symbol table, even if it's a valid one. If it
463 * _isn't_ a valid one, we should barf immediately.
465 if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
466 strcmp(name, "..got") && strcmp(name, "..plt") &&
467 strcmp(name, "..sym"))
468 error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
469 return;
472 if (is_global == 3) {
473 struct Symbol **s;
475 * Fix up a forward-reference symbol size from the first
476 * pass.
478 for (s = &fwds; *s; s = &(*s)->nextfwd)
479 if (!strcmp((*s)->name, name)) {
480 struct tokenval tokval;
481 expr *e;
482 char *p = special;
484 while (*p && !isspace(*p))
485 p++;
486 while (*p && isspace(*p))
487 p++;
488 stdscan_reset();
489 stdscan_bufptr = p;
490 tokval.t_type = TOKEN_INVALID;
491 e = evaluate(stdscan, NULL, &tokval, NULL, 1, error, NULL);
492 if (e) {
493 if (!is_simple(e))
494 error(ERR_NONFATAL, "cannot use relocatable"
495 " expression as symbol size");
496 else
497 (*s)->size = reloc_value(e);
501 * Remove it from the list of unresolved sizes.
503 nasm_free((*s)->name);
504 *s = (*s)->nextfwd;
505 return;
507 return; /* it wasn't an important one */
510 saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
511 strslen += 1 + strlen(name);
513 sym = saa_wstruct(syms);
515 sym->strpos = pos;
516 sym->type = is_global ? SYM_GLOBAL : 0;
517 sym->other = STV_DEFAULT;
518 sym->size = 0;
519 if (segment == NO_SEG)
520 sym->section = SHN_ABS;
521 else {
522 int i;
523 sym->section = SHN_UNDEF;
524 if (nsects == 0 && segment == def_seg) {
525 int tempint;
526 if (segment != elf_section_names(".text", 2, &tempint))
527 error(ERR_PANIC,
528 "strange segment conditions in ELF driver");
529 sym->section = nsects;
530 } else {
531 for (i = 0; i < nsects; i++)
532 if (segment == sects[i]->index) {
533 sym->section = i + 1;
534 break;
539 if (is_global == 2) {
540 sym->size = offset;
541 sym->value = 0;
542 sym->section = SHN_COMMON;
544 * We have a common variable. Check the special text to see
545 * if it's a valid number and power of two; if so, store it
546 * as the alignment for the common variable.
548 if (special) {
549 int err;
550 sym->value = readnum(special, &err);
551 if (err)
552 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
553 " valid number", special);
554 else if ((sym->value | (sym->value - 1)) != 2 * sym->value - 1)
555 error(ERR_NONFATAL, "alignment constraint `%s' is not a"
556 " power of two", special);
558 special_used = TRUE;
559 } else
560 sym->value = (sym->section == SHN_UNDEF ? 0 : offset);
562 if (sym->type == SYM_GLOBAL) {
564 * If sym->section == SHN_ABS, then the first line of the
565 * else section would cause a core dump, because its a reference
566 * beyond the end of the section array.
567 * This behaviour is exhibited by this code:
568 * GLOBAL crash_nasm
569 * crash_nasm equ 0
570 * To avoid such a crash, such requests are silently discarded.
571 * This may not be the best solution.
573 if (sym->section == SHN_UNDEF || sym->section == SHN_COMMON) {
574 bsym = raa_write(bsym, segment, nglobs);
575 } else if (sym->section != SHN_ABS) {
577 * This is a global symbol; so we must add it to the linked
578 * list of global symbols in its section. We'll push it on
579 * the beginning of the list, because it doesn't matter
580 * much which end we put it on and it's easier like this.
582 * In addition, we check the special text for symbol
583 * type and size information.
585 sym->next = sects[sym->section - 1]->gsyms;
586 sects[sym->section - 1]->gsyms = sym;
588 if (special) {
589 int n = strcspn(special, " \t");
591 if (!nasm_strnicmp(special, "function", n))
592 sym->type |= SYM_FUNCTION;
593 else if (!nasm_strnicmp(special, "data", n) ||
594 !nasm_strnicmp(special, "object", n))
595 sym->type |= SYM_DATA;
596 else if (!nasm_strnicmp(special, "notype", n))
597 sym->type |= SYM_NOTYPE;
598 else
599 error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
600 n, special);
601 special += n;
603 while (isspace(*special))
604 ++special;
605 if (*special) {
606 n = strcspn(special, " \t");
607 if (!nasm_strnicmp(special, "default", n))
608 sym->other = STV_DEFAULT;
609 else if (!nasm_strnicmp(special, "internal", n))
610 sym->other = STV_INTERNAL;
611 else if (!nasm_strnicmp(special, "hidden", n))
612 sym->other = STV_HIDDEN;
613 else if (!nasm_strnicmp(special, "protected", n))
614 sym->other = STV_PROTECTED;
615 else
616 n = 0;
617 special += n;
620 if (*special) {
621 struct tokenval tokval;
622 expr *e;
623 int fwd = FALSE;
624 char *saveme = stdscan_bufptr; /* bugfix? fbk 8/10/00 */
626 while (special[n] && isspace(special[n]))
627 n++;
629 * We have a size expression; attempt to
630 * evaluate it.
632 stdscan_reset();
633 stdscan_bufptr = special + n;
634 tokval.t_type = TOKEN_INVALID;
635 e = evaluate(stdscan, NULL, &tokval, &fwd, 0, error,
636 NULL);
637 if (fwd) {
638 sym->nextfwd = fwds;
639 fwds = sym;
640 sym->name = nasm_strdup(name);
641 } else if (e) {
642 if (!is_simple(e))
643 error(ERR_NONFATAL, "cannot use relocatable"
644 " expression as symbol size");
645 else
646 sym->size = reloc_value(e);
648 stdscan_bufptr = saveme; /* bugfix? fbk 8/10/00 */
650 special_used = TRUE;
653 sym->globnum = nglobs;
654 nglobs++;
655 } else
656 nlocals++;
658 if (special && !special_used)
659 error(ERR_NONFATAL, "no special symbol features supported here");
662 static void elf_add_reloc(struct Section *sect, int32_t segment, int type)
664 struct Reloc *r;
666 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
667 sect->tail = &r->next;
668 r->next = NULL;
670 r->address = sect->len;
671 if (segment == NO_SEG)
672 r->symbol = 2;
673 else {
674 int i;
675 r->symbol = 0;
676 for (i = 0; i < nsects; i++)
677 if (segment == sects[i]->index)
678 r->symbol = i + 3;
679 if (!r->symbol)
680 r->symbol = GLOBAL_TEMP_BASE + raa_read(bsym, segment);
682 r->type = type;
684 sect->nrelocs++;
688 * This routine deals with ..got and ..sym relocations: the more
689 * complicated kinds. In shared-library writing, some relocations
690 * with respect to global symbols must refer to the precise symbol
691 * rather than referring to an offset from the base of the section
692 * _containing_ the symbol. Such relocations call to this routine,
693 * which searches the symbol list for the symbol in question.
695 * R_386_GOT32 references require the _exact_ symbol address to be
696 * used; R_386_32 references can be at an offset from the symbol.
697 * The boolean argument `exact' tells us this.
699 * Return value is the adjusted value of `addr', having become an
700 * offset from the symbol rather than the section. Should always be
701 * zero when returning from an exact call.
703 * Limitation: if you define two symbols at the same place,
704 * confusion will occur.
706 * Inefficiency: we search, currently, using a linked list which
707 * isn't even necessarily sorted.
709 static int32_t elf_add_gsym_reloc(struct Section *sect,
710 int32_t segment, int32_t offset,
711 int type, int exact)
713 struct Reloc *r;
714 struct Section *s;
715 struct Symbol *sym, *sm;
716 int i;
719 * First look up the segment/offset pair and find a global
720 * symbol corresponding to it. If it's not one of our segments,
721 * then it must be an external symbol, in which case we're fine
722 * doing a normal elf_add_reloc after first sanity-checking
723 * that the offset from the symbol is zero.
725 s = NULL;
726 for (i = 0; i < nsects; i++)
727 if (segment == sects[i]->index) {
728 s = sects[i];
729 break;
731 if (!s) {
732 if (exact && offset != 0)
733 error(ERR_NONFATAL, "unable to find a suitable global symbol"
734 " for this reference");
735 else
736 elf_add_reloc(sect, segment, type);
737 return offset;
740 if (exact) {
742 * Find a symbol pointing _exactly_ at this one.
744 for (sym = s->gsyms; sym; sym = sym->next)
745 if (sym->value == offset)
746 break;
747 } else {
749 * Find the nearest symbol below this one.
751 sym = NULL;
752 for (sm = s->gsyms; sm; sm = sm->next)
753 if (sm->value <= offset && (!sym || sm->value > sym->value))
754 sym = sm;
756 if (!sym && exact) {
757 error(ERR_NONFATAL, "unable to find a suitable global symbol"
758 " for this reference");
759 return 0;
762 r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
763 sect->tail = &r->next;
764 r->next = NULL;
766 r->address = sect->len;
767 r->symbol = GLOBAL_TEMP_BASE + sym->globnum;
768 r->type = type;
770 sect->nrelocs++;
772 return offset - sym->value;
775 static void elf_out(int32_t segto, const void *data, uint32_t type,
776 int32_t segment, int32_t wrt)
778 struct Section *s;
779 int32_t realbytes = type & OUT_SIZMASK;
780 int32_t addr;
781 uint8_t mydata[16], *p;
782 int i;
783 static struct symlininfo sinfo;
785 type &= OUT_TYPMASK;
787 #if defined(DEBUG) && DEBUG>2
788 fprintf(stderr,
789 " elf_out type: %x seg: %d bytes: %x data: %x\n",
790 (type >> 24), segment, realbytes, *(int32_t *)data);
791 #endif
794 * handle absolute-assembly (structure definitions)
796 if (segto == NO_SEG) {
797 if (type != OUT_RESERVE)
798 error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
799 " space");
800 return;
803 s = NULL;
804 for (i = 0; i < nsects; i++)
805 if (segto == sects[i]->index) {
806 s = sects[i];
807 break;
809 if (!s) {
810 int tempint; /* ignored */
811 if (segto != elf_section_names(".text", 2, &tempint))
812 error(ERR_PANIC, "strange segment conditions in ELF driver");
813 else {
814 s = sects[nsects - 1];
815 i = nsects - 1;
819 /* again some stabs debugging stuff */
820 if (of_elf64.current_dfmt) {
821 sinfo.offset = s->len;
822 sinfo.section = i;
823 sinfo.name = s->name;
824 of_elf64.current_dfmt->debug_output(TY_STABSSYMLIN, &sinfo);
826 /* end of debugging stuff */
828 if (s->type == SHT_NOBITS && type != OUT_RESERVE) {
829 error(ERR_WARNING, "attempt to initialize memory in"
830 " BSS section `%s': ignored", s->name);
831 if (type == OUT_REL2ADR)
832 realbytes = 2;
833 else if (type == OUT_REL4ADR)
834 realbytes = 4;
835 s->len += realbytes;
836 return;
839 if (type == OUT_RESERVE) {
840 if (s->type == SHT_PROGBITS) {
841 error(ERR_WARNING, "uninitialized space declared in"
842 " non-BSS section `%s': zeroing", s->name);
843 elf_sect_write(s, NULL, realbytes);
844 } else
845 s->len += realbytes;
846 } else if (type == OUT_RAWDATA) {
847 if (segment != NO_SEG)
848 error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
849 elf_sect_write(s, data, realbytes);
850 } else if (type == OUT_ADDRESS) {
851 int gnu16 = 0;
852 addr = *(int32_t *)data;
853 if (segment != NO_SEG) {
854 if (segment % 2) {
855 error(ERR_NONFATAL, "ELF format does not support"
856 " segment base references");
857 } else {
858 if (wrt == NO_SEG) {
859 if (realbytes == 2) {
860 gnu16 = 1;
861 elf_add_reloc(s, segment, R_X86_64_16);
862 } else {
863 elf_add_reloc(s, segment, R_X86_64_32);
865 } else if (wrt == elf_gotpc_sect + 1) {
867 * The user will supply GOT relative to $$. ELF
868 * will let us have GOT relative to $. So we
869 * need to fix up the data item by $-$$.
871 addr += s->len;
872 elf_add_reloc(s, segment, R_X86_64_GOTPCREL);
873 } else if (wrt == elf_gotoff_sect + 1) {
874 elf_add_reloc(s, segment, R_X86_64_GOTTPOFF);
875 } else if (wrt == elf_got_sect + 1) {
876 addr = elf_add_gsym_reloc(s, segment, addr,
877 R_X86_64_GOT32, TRUE);
878 } else if (wrt == elf_sym_sect + 1) {
879 if (realbytes == 2) {
880 gnu16 = 1;
881 addr = elf_add_gsym_reloc(s, segment, addr,
882 R_X86_64_16, FALSE);
883 } else {
884 addr = elf_add_gsym_reloc(s, segment, addr,
885 R_X86_64_32, FALSE);
887 } else if (wrt == elf_plt_sect + 1) {
888 error(ERR_NONFATAL, "ELF format cannot produce non-PC-"
889 "relative PLT references");
890 } else {
891 error(ERR_NONFATAL, "ELF format does not support this"
892 " use of WRT");
893 wrt = NO_SEG; /* we can at least _try_ to continue */
897 p = mydata;
898 if (gnu16) {
899 WRITESHORT(p, addr);
900 } else {
901 if (realbytes != 8 && realbytes != 4 && segment != NO_SEG) {
902 error(ERR_NONFATAL,
903 "Unsupported non-64-bit ELF relocation");
905 if (realbytes == 4) WRITELONG(p, addr);
906 else WRITEDLONG(p, (int64_t)addr);
908 elf_sect_write(s, mydata, realbytes);
909 } else if (type == OUT_REL2ADR) {
910 if (segment == segto)
911 error(ERR_PANIC, "intra-segment OUT_REL2ADR");
912 if (segment != NO_SEG && segment % 2) {
913 error(ERR_NONFATAL, "ELF format does not support"
914 " segment base references");
915 } else {
916 if (wrt == NO_SEG) {
917 error(ERR_WARNING | ERR_WARN_GNUELF,
918 "16-bit relocations in ELF is a GNU extension");
919 elf_add_reloc(s, segment, R_X86_64_PC16);
920 } else {
921 error(ERR_NONFATAL,
922 "Unsupported non-32-bit ELF relocation [2]");
925 p = mydata;
926 WRITESHORT(p, *(int32_t *)data - realbytes);
927 elf_sect_write(s, mydata, 2L);
928 } else if (type == OUT_REL4ADR) {
929 if (segment == segto)
930 error(ERR_PANIC, "intra-segment OUT_REL4ADR");
931 if (segment != NO_SEG && segment % 2) {
932 error(ERR_NONFATAL, "ELF format does not support"
933 " segment base references");
934 } else {
935 if (wrt == NO_SEG) {
936 elf_add_reloc(s, segment, R_X86_64_PC32);
937 } else if (wrt == elf_plt_sect + 1) {
938 elf_add_reloc(s, segment, R_X86_64_PLT32);
939 } else if (wrt == elf_gotpc_sect + 1 ||
940 wrt == elf_gotoff_sect + 1 ||
941 wrt == elf_got_sect + 1) {
942 error(ERR_NONFATAL, "ELF format cannot produce PC-"
943 "relative GOT references");
944 } else {
945 error(ERR_NONFATAL, "ELF format does not support this"
946 " use of WRT");
947 wrt = NO_SEG; /* we can at least _try_ to continue */
950 p = mydata;
951 WRITELONG(p, *(int32_t *)data - realbytes);
952 elf_sect_write(s, mydata, 4L);
956 static void elf_write(void)
958 int nsections, align;
959 int scount;
960 char *p;
961 int commlen;
962 char comment[64];
963 int i;
965 struct SAA *symtab;
966 int32_t symtablen, symtablocal;
969 * Work out how many sections we will have. We have SHN_UNDEF,
970 * then the flexible user sections, then the four fixed
971 * sections `.comment', `.shstrtab', `.symtab' and `.strtab',
972 * then optionally relocation sections for the user sections.
974 if (of_elf64.current_dfmt == &df_stabs)
975 nsections = 8;
976 else
977 nsections = 5; /* SHN_UNDEF and the fixed ones */
979 add_sectname("", ".comment");
980 add_sectname("", ".shstrtab");
981 add_sectname("", ".symtab");
982 add_sectname("", ".strtab");
983 for (i = 0; i < nsects; i++) {
984 nsections++; /* for the section itself */
985 if (sects[i]->head) {
986 nsections++; /* for its relocations without addends*/
987 add_sectname(".rela", sects[i]->name);
991 if (of_elf64.current_dfmt == &df_stabs) {
992 /* in case the debug information is wanted, just add these three sections... */
993 add_sectname("", ".stab");
994 add_sectname("", ".stabstr");
995 add_sectname(".rel", ".stab");
999 * Do the comment.
1001 *comment = '\0';
1002 commlen =
1003 2 + sprintf(comment + 1, "The Netwide Assembler %s", NASM_VER);
1006 * Output the ELF header.
1008 fwrite("\177ELF\2\1\1\0\0\0\0\0\0\0\0\0", 16, 1, elffp);
1009 fwriteint16_t(ET_REL, elffp); /* relocatable file */
1010 fwriteint16_t(EM_X86_64, elffp); /* processor ID */
1011 fwriteint32_t(1L, elffp); /* EV_CURRENT file format version */
1012 fwriteint64_t(0L, elffp); /* no entry point */
1013 fwriteint64_t(0L, elffp); /* no program header table */
1014 fwriteint64_t(0x40L, elffp); /* section headers straight after
1015 * ELF header plus alignment */
1016 fwriteint32_t(0L, elffp); /* 386 defines no special flags */
1017 fwriteint16_t(0x40, elffp); /* size of ELF header */
1018 fwriteint16_t(0, elffp); /* no program header table, again */
1019 fwriteint16_t(0, elffp); /* still no program header table */
1020 fwriteint16_t(sizeof(Elf64_Shdr), elffp); /* size of section header */
1021 fwriteint16_t(nsections, elffp); /* number of sections */
1022 fwriteint16_t(nsects + 2, elffp); /* string table section index for
1023 * section header table */
1026 * Build the symbol table and relocation tables.
1028 symtab = elf_build_symtab(&symtablen, &symtablocal);
1029 for (i = 0; i < nsects; i++)
1030 if (sects[i]->head)
1031 sects[i]->rel = elf_build_reltab(&sects[i]->rellen,
1032 sects[i]->head);
1035 * Now output the section header table.
1038 elf_foffs = 0x40 + sizeof(Elf64_Shdr) * nsections;
1039 align = ((elf_foffs + SEG_ALIGN_1) & ~SEG_ALIGN_1) - elf_foffs;
1040 elf_foffs += align;
1041 elf_nsect = 0;
1042 elf_sects = nasm_malloc(sizeof(*elf_sects) * (2 * nsects + 10));
1044 elf_section_header(0, 0, 0, NULL, FALSE, 0L, 0, 0, 0, 0); /* SHN_UNDEF */
1045 scount = 1; /* needed for the stabs debugging to track the symtable section */
1046 p = shstrtab + 1;
1047 for (i = 0; i < nsects; i++) {
1048 elf_section_header(p - shstrtab, sects[i]->type, sects[i]->flags,
1049 (sects[i]->type == SHT_PROGBITS ?
1050 sects[i]->data : NULL), TRUE,
1051 sects[i]->len, 0, 0, sects[i]->align, 0);
1052 p += strlen(p) + 1;
1053 scount++; /* dito */
1055 elf_section_header(p - shstrtab, 1, 0, comment, FALSE, (int32_t)commlen, 0, 0, 1, 0); /* .comment */
1056 scount++; /* dito */
1057 p += strlen(p) + 1;
1058 elf_section_header(p - shstrtab, 3, 0, shstrtab, FALSE, (int32_t)shstrtablen, 0, 0, 1, 0); /* .shstrtab */
1059 scount++; /* dito */
1060 p += strlen(p) + 1;
1061 elf_section_header(p - shstrtab, 2, 0, symtab, TRUE, symtablen, nsects + 4, symtablocal, 4, 24); /* .symtab */
1062 symtabsection = scount; /* now we got the symtab section index in the ELF file */
1063 p += strlen(p) + 1;
1064 elf_section_header(p - shstrtab, 3, 0, strs, TRUE, strslen, 0, 0, 1, 0); /* .strtab */
1065 for (i = 0; i < nsects; i++)
1066 if (sects[i]->head) {
1067 p += strlen(p) + 1;
1068 elf_section_header(p - shstrtab,SHT_RELA, 0, sects[i]->rel, TRUE,
1069 sects[i]->rellen, nsects + 3, i + 1, 4, 24);
1071 if (of_elf64.current_dfmt == &df_stabs) {
1072 /* for debugging information, create the last three sections
1073 which are the .stab , .stabstr and .rel.stab sections respectively */
1075 /* this function call creates the stab sections in memory */
1076 stabs64_generate();
1078 if ((stabbuf) && (stabstrbuf) && (stabrelbuf)) {
1079 p += strlen(p) + 1;
1080 elf_section_header(p - shstrtab, 1, 0, stabbuf, 0, stablen,
1081 nsections - 2, 0, 4, 12);
1083 p += strlen(p) + 1;
1084 elf_section_header(p - shstrtab, 3, 0, stabstrbuf, 0,
1085 stabstrlen, 0, 0, 4, 0);
1087 p += strlen(p) + 1;
1088 /* link -> symtable info -> section to refer to */
1089 elf_section_header(p - shstrtab, 9, 0, stabrelbuf, 0,
1090 stabrellen, symtabsection, nsections - 3, 4,
1091 16);
1094 fwrite(align_str, align, 1, elffp);
1097 * Now output the sections.
1099 elf_write_sections();
1101 nasm_free(elf_sects);
1102 saa_free(symtab);
1105 static struct SAA *elf_build_symtab(int32_t *len, int32_t *local)
1107 struct SAA *s = saa_init(1L);
1108 struct Symbol *sym;
1109 uint8_t entry[24], *p;
1110 int i;
1112 *len = *local = 0;
1115 * First, an all-zeros entry, required by the ELF spec.
1117 saa_wbytes(s, NULL, 24L); /* null symbol table entry */
1118 *len += 24;
1119 (*local)++;
1122 * Next, an entry for the file name.
1124 p = entry;
1125 WRITELONG(p, 1); /* we know it's 1st thing in strtab */
1126 WRITESHORT(p, 4); /* type FILE */
1127 WRITESHORT(p, SHN_ABS);
1128 WRITEDLONG(p, (uint64_t) 0); /* no value */
1129 WRITEDLONG(p, (uint64_t) 0); /* no size either */
1130 saa_wbytes(s, entry, 24L);
1131 *len += 24;
1132 (*local)++;
1135 * Now some standard symbols defining the segments, for relocation
1136 * purposes.
1138 for (i = 1; i <= nsects + 1; i++) {
1139 p = entry;
1140 WRITELONG(p, 0); /* no symbol name */
1141 WRITESHORT(p, 3); /* local section-type thing */
1142 WRITESHORT(p, (i == 1 ? SHN_ABS : i - 1)); /* the section id */
1143 WRITEDLONG(p, (uint64_t) 0); /* offset zero */
1144 WRITEDLONG(p, (uint64_t) 0); /* size zero */
1145 saa_wbytes(s, entry, 24L);
1146 *len += 24;
1147 (*local)++;
1151 * Now the other local symbols.
1153 saa_rewind(syms);
1154 while ((sym = saa_rstruct(syms))) {
1155 if (sym->type & SYM_GLOBAL)
1156 continue;
1157 p = entry;
1158 WRITELONG(p, sym->strpos);
1159 WRITECHAR(p, sym->type); /* local non-typed thing */
1160 WRITECHAR(p, sym->other);
1161 WRITESHORT(p, sym->section);
1162 WRITEDLONG(p, (int64_t)sym->value);
1163 WRITEDLONG(p, (int64_t)sym->size);
1164 saa_wbytes(s, entry, 24L);
1165 *len += 24;
1166 (*local)++;
1170 * Now the global symbols.
1172 saa_rewind(syms);
1173 while ((sym = saa_rstruct(syms))) {
1174 if (!(sym->type & SYM_GLOBAL))
1175 continue;
1176 p = entry;
1177 WRITELONG(p, sym->strpos);
1178 WRITECHAR(p, sym->type); /* global non-typed thing */
1179 WRITECHAR(p, sym->other);
1180 WRITESHORT(p, sym->section);
1181 WRITEDLONG(p, (int64_t)sym->value);
1182 WRITEDLONG(p, (int64_t)sym->size);
1183 saa_wbytes(s, entry, 24L);
1184 *len += 24;
1187 return s;
1190 static struct SAA *elf_build_reltab(int32_t *len, struct Reloc *r)
1192 struct SAA *s;
1193 uint8_t *p, entry[24];
1195 if (!r)
1196 return NULL;
1198 s = saa_init(1L);
1199 *len = 0;
1201 while (r) {
1202 int64_t sym = r->symbol;
1204 if (sym >= GLOBAL_TEMP_BASE)
1205 sym += -GLOBAL_TEMP_BASE + (nsects + 3) + nlocals;
1207 p = entry;
1208 WRITEDLONG(p, r->address);
1209 WRITEDLONG(p, (sym << 32) + r->type);
1210 WRITEDLONG(p, (uint64_t) 0);
1211 saa_wbytes(s, entry, 24L);
1212 *len += 24;
1214 r = r->next;
1217 return s;
1220 static void elf_section_header(int name, int type, int flags,
1221 void *data, int is_saa, int32_t datalen,
1222 int link, int info, int align, int eltsize)
1224 elf_sects[elf_nsect].data = data;
1225 elf_sects[elf_nsect].len = datalen;
1226 elf_sects[elf_nsect].is_saa = is_saa;
1227 elf_nsect++;
1229 fwriteint32_t((int32_t)name, elffp);
1230 fwriteint32_t((int32_t)type, elffp);
1231 fwriteint64_t((int64_t)flags, elffp);
1232 fwriteint64_t(0L, elffp); /* no address, ever, in object files */
1233 fwriteint64_t(type == 0 ? 0L : elf_foffs, elffp);
1234 fwriteint64_t(datalen, elffp);
1235 if (data)
1236 elf_foffs += (datalen + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1237 fwriteint32_t((int32_t)link, elffp);
1238 fwriteint32_t((int32_t)info, elffp);
1239 fwriteint64_t((int64_t)align, elffp);
1240 fwriteint64_t((int64_t)eltsize, elffp);
1243 static void elf_write_sections(void)
1245 int i;
1246 for (i = 0; i < elf_nsect; i++)
1247 if (elf_sects[i].data) {
1248 int32_t len = elf_sects[i].len;
1249 int32_t reallen = (len + SEG_ALIGN_1) & ~SEG_ALIGN_1;
1250 int32_t align = reallen - len;
1251 if (elf_sects[i].is_saa)
1252 saa_fpwrite(elf_sects[i].data, elffp);
1253 else
1254 fwrite(elf_sects[i].data, len, 1, elffp);
1255 fwrite(align_str, align, 1, elffp);
1259 static void elf_sect_write(struct Section *sect,
1260 const uint8_t *data, uint32_t len)
1262 saa_wbytes(sect->data, data, len);
1263 sect->len += len;
1266 static int32_t elf_segbase(int32_t segment)
1268 return segment;
1271 static int elf_directive(char *directive, char *value, int pass)
1273 (void)directive;
1274 (void)value;
1275 (void)pass;
1276 return 0;
1279 static void elf_filename(char *inname, char *outname, efunc error)
1281 strcpy(elf_module, inname);
1282 standard_extension(inname, outname, ".o", error);
1285 static const char *elf_stdmac[] = {
1286 "%define __SECT__ [section .text]",
1287 "%macro __NASM_CDecl__ 1",
1288 "%define $_%1 $%1",
1289 "%endmacro",
1290 NULL
1292 static int elf_set_info(enum geninfo type, char **val)
1294 (void)type;
1295 (void)val;
1296 return 0;
1299 static struct dfmt df_stabs = {
1300 "ELF64 (X86_64) stabs debug format for Linux",
1301 "stabs",
1302 stabs64_init,
1303 stabs64_linenum,
1304 stabs64_deflabel,
1305 stabs64_directive,
1306 stabs64_typevalue,
1307 stabs64_output,
1308 stabs64_cleanup
1311 struct dfmt *elf64_debugs_arr[2] = { &df_stabs, NULL };
1313 struct ofmt of_elf64 = {
1314 "ELF64 (x86_64) object files (e.g. Linux)",
1315 "elf64",
1316 NULL,
1317 elf64_debugs_arr,
1318 &null_debug_form,
1319 elf_stdmac,
1320 elf_init,
1321 elf_set_info,
1322 elf_out,
1323 elf_deflabel,
1324 elf_section_names,
1325 elf_segbase,
1326 elf_directive,
1327 elf_filename,
1328 elf_cleanup
1331 /* again, the stabs debugging stuff (code) */
1333 void stabs64_init(struct ofmt *of, void *id, FILE * fp, efunc error)
1335 (void)of;
1336 (void)id;
1337 (void)fp;
1338 (void)error;
1341 void stabs64_linenum(const char *filename, int32_t linenumber, int32_t segto)
1343 (void)segto;
1345 if (!stabs_filename) {
1346 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1347 strcpy(stabs_filename, filename);
1348 } else {
1349 if (strcmp(stabs_filename, filename)) {
1350 /* yep, a memory leak...this program is one-shot anyway, so who cares...
1351 in fact, this leak comes in quite handy to maintain a list of files
1352 encountered so far in the symbol lines... */
1354 /* why not nasm_free(stabs_filename); we're done with the old one */
1356 stabs_filename = (char *)nasm_malloc(strlen(filename) + 1);
1357 strcpy(stabs_filename, filename);
1360 stabs_immcall = 1;
1361 currentline = linenumber;
1364 void stabs64_deflabel(char *name, int32_t segment, int32_t offset, int is_global,
1365 char *special)
1367 (void)name;
1368 (void)segment;
1369 (void)offset;
1370 (void)is_global;
1371 (void)special;
1374 void stabs64_directive(const char *directive, const char *params)
1376 (void)directive;
1377 (void)params;
1380 void stabs64_typevalue(int32_t type)
1382 (void)type;
1385 void stabs64_output(int type, void *param)
1387 struct symlininfo *s;
1388 struct linelist *el;
1389 if (type == TY_STABSSYMLIN) {
1390 if (stabs_immcall) {
1391 s = (struct symlininfo *)param;
1392 if (strcmp(s->name, ".text"))
1393 return; /* we are only interested in the text stuff */
1394 numlinestabs++;
1395 el = (struct linelist *)nasm_malloc(sizeof(struct linelist));
1396 el->info.offset = s->offset;
1397 el->info.section = s->section;
1398 el->info.name = s->name;
1399 el->line = currentline;
1400 el->filename = stabs_filename;
1401 el->next = 0;
1402 if (stabslines) {
1403 stabslines->last->next = el;
1404 stabslines->last = el;
1405 } else {
1406 stabslines = el;
1407 stabslines->last = el;
1411 stabs_immcall = 0;
1414 #define WRITE_STAB(p,n_strx,n_type,n_other,n_desc,n_value) \
1415 do {\
1416 WRITELONG(p,n_strx); \
1417 WRITECHAR(p,n_type); \
1418 WRITECHAR(p,n_other); \
1419 WRITESHORT(p,n_desc); \
1420 WRITELONG(p,n_value); \
1421 } while (0)
1423 /* for creating the .stab , .stabstr and .rel.stab sections in memory */
1425 void stabs64_generate(void)
1427 int i, numfiles, strsize, numstabs = 0, currfile, mainfileindex;
1428 uint8_t *sbuf, *ssbuf, *rbuf, *sptr, *rptr;
1429 char **allfiles;
1430 int *fileidx;
1432 struct linelist *ptr;
1434 ptr = stabslines;
1436 allfiles = (char **)nasm_malloc(numlinestabs * sizeof(int8_t *));
1437 for (i = 0; i < numlinestabs; i++)
1438 allfiles[i] = 0;
1439 numfiles = 0;
1440 while (ptr) {
1441 if (numfiles == 0) {
1442 allfiles[0] = ptr->filename;
1443 numfiles++;
1444 } else {
1445 for (i = 0; i < numfiles; i++) {
1446 if (!strcmp(allfiles[i], ptr->filename))
1447 break;
1449 if (i >= numfiles) {
1450 allfiles[i] = ptr->filename;
1451 numfiles++;
1454 ptr = ptr->next;
1456 strsize = 1;
1457 fileidx = (int *)nasm_malloc(numfiles * sizeof(int));
1458 for (i = 0; i < numfiles; i++) {
1459 fileidx[i] = strsize;
1460 strsize += strlen(allfiles[i]) + 1;
1462 mainfileindex = 0;
1463 for (i = 0; i < numfiles; i++) {
1464 if (!strcmp(allfiles[i], elf_module)) {
1465 mainfileindex = i;
1466 break;
1470 /* worst case size of the stab buffer would be:
1471 the sourcefiles changes each line, which would mean 1 SOL, 1 SYMLIN per line
1473 sbuf =
1474 (uint8_t *)nasm_malloc((numlinestabs * 2 + 3) *
1475 sizeof(struct stabentry));
1477 ssbuf = (uint8_t *)nasm_malloc(strsize);
1479 rbuf = (uint8_t *)nasm_malloc(numlinestabs * 16 * (2 + 3));
1480 rptr = rbuf;
1482 for (i = 0; i < numfiles; i++) {
1483 strcpy((char *)ssbuf + fileidx[i], allfiles[i]);
1485 ssbuf[0] = 0;
1487 stabstrlen = strsize; /* set global variable for length of stab strings */
1489 sptr = sbuf;
1490 /* this is the first stab, its strx points to the filename of the
1491 the source-file, the n_desc field should be set to the number
1492 of remaining stabs
1494 WRITE_STAB(sptr, fileidx[0], 0, 0, 0, strlen(allfiles[0] + 12));
1496 ptr = stabslines;
1497 numstabs = 0;
1499 if (ptr) {
1500 /* this is the stab for the main source file */
1501 WRITE_STAB(sptr, fileidx[mainfileindex], N_SO, 0, 0, 0);
1503 /* relocation table entry */
1505 /* Since the above WRITE_STAB calls have already */
1506 /* created two entries, the index in the info.section */
1507 /* member must be adjusted by adding 3 */
1509 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1510 WRITEDLONG(rptr, ((int64_t)(ptr->info.section + 3) << 32) | R_X86_64_32);
1512 numstabs++;
1513 currfile = mainfileindex;
1516 while (ptr) {
1517 if (strcmp(allfiles[currfile], ptr->filename)) {
1518 /* oops file has changed... */
1519 for (i = 0; i < numfiles; i++)
1520 if (!strcmp(allfiles[i], ptr->filename))
1521 break;
1522 currfile = i;
1523 WRITE_STAB(sptr, fileidx[currfile], N_SOL, 0, 0,
1524 ptr->info.offset);
1525 numstabs++;
1527 /* relocation table entry */
1529 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1530 WRITEDLONG(rptr, ((int64_t)(ptr->info.section + 3) << 32) | R_X86_64_32);
1533 WRITE_STAB(sptr, 0, N_SLINE, 0, ptr->line, ptr->info.offset);
1534 numstabs++;
1536 /* relocation table entry */
1538 WRITEDLONG(rptr, (int64_t)(sptr - sbuf) - 4);
1539 WRITEDLONG(rptr, ((int64_t)(ptr->info.section + 3) << 32) | R_X86_64_32);
1541 ptr = ptr->next;
1545 ((struct stabentry *)sbuf)->n_desc = numstabs;
1547 nasm_free(allfiles);
1548 nasm_free(fileidx);
1550 stablen = (sptr - sbuf);
1551 stabrellen = (rptr - rbuf);
1552 stabrelbuf = rbuf;
1553 stabbuf = sbuf;
1554 stabstrbuf = ssbuf;
1557 void stabs64_cleanup(void)
1559 struct linelist *ptr, *del;
1560 if (!stabslines)
1561 return;
1562 ptr = stabslines;
1563 while (ptr) {
1564 del = ptr;
1565 ptr = ptr->next;
1566 nasm_free(del);
1568 if (stabbuf)
1569 nasm_free(stabbuf);
1570 if (stabrelbuf)
1571 nasm_free(stabrelbuf);
1572 if (stabstrbuf)
1573 nasm_free(stabstrbuf);
1576 #endif /* OF_ELF */