1 // Derived from Inferno utils/6l/l.h and related files.
2 // http://code.google.com/p/inferno-os/source/browse/utils/6l/l.h
4 // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
5 // Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
6 // Portions Copyright © 1997-1999 Vita Nuova Limited
7 // Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
8 // Portions Copyright © 2004,2006 Bruce Ellis
9 // Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
10 // Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
11 // Portions Copyright © 2009 The Go Authors. All rights reserved.
13 // Permission is hereby granted, free of charge, to any person obtaining a copy
14 // of this software and associated documentation files (the "Software"), to deal
15 // in the Software without restriction, including without limitation the rights
16 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 // copies of the Software, and to permit persons to whom the Software is
18 // furnished to do so, subject to the following conditions:
20 // The above copyright notice and this permission notice shall be included in
21 // all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 typedef struct Addr Addr;
32 typedef struct Prog Prog;
33 typedef struct LSym LSym;
34 typedef struct Reloc Reloc;
35 typedef struct Auto Auto;
36 typedef struct Hist Hist;
37 typedef struct Link Link;
38 typedef struct Plist Plist;
39 typedef struct LinkArch LinkArch;
40 typedef struct Library Library;
42 typedef struct Pcln Pcln;
43 typedef struct Pcdata Pcdata;
44 typedef struct Pciter Pciter;
46 // An Addr is an argument to an instruction.
47 // The general forms and their encodings are:
49 // sym±offset(symkind)(reg)(index*scale)
50 // Memory reference at address &sym(symkind) + offset + reg + index*scale.
51 // Any of sym(symkind), ±offset, (reg), (index*scale), and *scale can be omitted.
52 // If (reg) and *scale are both omitted, the resulting expression (index) is parsed as (reg).
53 // To force a parsing as index*scale, write (index*1).
56 // name = symkind (NAME_AUTO, ...) or 0 (NAME_NONE)
60 // index = index (REG_*)
61 // scale = scale (1, 2, 4, 8)
64 // Effective address of memory reference <mem>, defined above.
65 // Encoding: same as memory reference, but type = TYPE_ADDR.
68 // This is a special case of $<mem>, in which only ±offset is present.
69 // It has a separate type for easy recognition.
72 // offset = ±integer value
75 // Indirect reference through memory reference <mem>, defined above.
76 // Only used on x86 for CALL/JMP *sym(SB), which calls/jumps to a function
77 // pointer stored in the data word sym(SB), not a function named sym(SB).
78 // Encoding: same as above, but type = TYPE_INDIR.
82 // On machines with actual SB registers, $*$<mem> forced the
83 // instruction encoding to use a full 32-bit constant, never a
84 // reference relative to SB.
86 // $<floating point literal>
87 // Floating point constant value.
90 // u.dval = floating point value
92 // $<string literal, up to 8 chars>
93 // String literal value (raw bytes used for DATA instruction).
99 // Any register: integer, floating point, control, segment, and so on.
100 // If looking for specific register kind, must check type and reg value range.
107 // type = TYPE_BRANCH
108 // u.branch = Prog* reference OR ELSE offset = target pc (branch takes priority)
111 // Final argument to TEXT, specifying local frame size x and argument size y.
112 // In this form, x and y are integer literals only, not arbitrary expressions.
113 // This avoids parsing ambiguities due to the use of - as a separator.
114 // The ± are optional.
115 // If the final argument to TEXT omits the -±y, the encoding should still
116 // use TYPE_TEXTSIZE (not TYPE_CONST), with u.argsize = ArgsSizeUnknown.
118 // type = TYPE_TEXTSIZE
122 // reg<<shift, reg>>shift, reg->shift, reg@>shift
123 // Shifted register value, for ARM.
124 // In this form, reg must be a register and shift can be a register or an integer constant.
127 // offset = (reg&15) | shifttype<<5 | count
128 // shifttype = 0, 1, 2, 3 for <<, >>, ->, @>
129 // count = (reg&15)<<8 | 1<<4 for a register shift count, (n&31)<<7 for an integer constant.
132 // A destination register pair. When used as the last argument of an instruction,
133 // this form makes clear that both registers are destinations.
135 // type = TYPE_REGREG
136 // reg = first register
137 // offset = second register
140 // TYPE_REGREG2, to be removed.
144 int16 type; // could be int8
157 int32 argsize; // for 5l, 8l
158 uint64 bits; // raw union bits, for testing if anything has been written to any field
161 // gotype is the name of the Go type descriptor for sym.
162 // It cannot be set using assembly syntax.
163 // It is generated by the Go compiler for global declarations,
164 // to convey information about pointer locations to the back end
165 // and for use in generating debug information.
168 int8 class; // for internal use by liblink
169 uint8 etype; // for internal use by 5g, 6g, 8g
170 void* node; // for internal use by 5g, 6g, 8g
171 int64 width; // for internal use by 5g, 6g, 8g
184 TYPE_BRANCH = 5, // avoid accidental conflicts with NAME_*
204 int32 variant; // RV_*: variant on computed value
211 // TODO(rsc): Describe prog.
212 // TODO(rsc): Describe TEXT/GLOBL flag in from3, DATA width in from3.
219 uchar scond; // arm only; condition codes
223 int16 reg; // arm, ppc64 only (e.g., ADD from, reg, to);
224 // starts at 0 for both GPRs and FPRs;
225 // also used for ADATA width on arm, ppc64
226 Addr from3; // addl source argument (e.g., RLWM/FMADD from, reg, from3, to)
229 // for 5g, 6g, 8g internal use
232 // for liblink internal use
235 Prog* comefrom; // amd64, 386
239 uint16 optab; // arm, ppc64
240 uchar back; // amd64, 386
241 uchar ft; // oclass cache
242 uchar tt; // oclass cache
243 uchar isize; // amd64, 386
245 char width; /* fake for DATA */
246 char mode; /* 16, 32, or 64 in 6l, 8l; internal use in 5g, 6g, 8g */
249 extern Prog zprog; // zeroed Prog
252 // These are the portable opcodes, common to all architectures.
253 // Each architecture defines many more arch-specific opcodes,
254 // with values starting at A_ARCHSPECIFIC.
277 A_ARCHSPECIFIC, // first architecture-specific opcode value
280 // prevent incompatible type signatures between liblink and 8l on Plan 9
281 #pragma incomplete struct Section
286 char* extname; // name used in external object files
298 uchar leaf; // arm only
299 uchar fnptr; // arm only
300 uchar localentry; // ppc64: instrs between global & local entry
302 uchar onlist; // on the textp or datap lists
303 int16 symid; // for writing .5/.6/.8 files
308 int32 align; // if non-zero, required alignment in bytes
310 int32 args; // size of stack frame incoming arguments area
311 int32 locals; // size of stack frame locals area (arm only?)
314 LSym* hash; // in hash table
315 LSym* allsym; // in all symbol list
316 LSym* next; // in text or data list
317 LSym* sub; // in SSUB list
318 LSym* outer; // container of sub
325 struct Section* sect;
347 /* order here is order in output file */
348 /* readonly, executable */
352 /* readonly, non-executable */
360 SSYMTAB, // TODO: move to unmapped section
364 /* writable, non-executable */
367 SMACHO, /* Mach-O __nl_symbol_ptr */
370 SELFGOT, /* also .toc in ppc64 ABI */
390 SSUB = 1<<8, /* sub-symbol, linked from parent via ->sub list */
392 SHIDDEN = 1<<9, // hidden or local symbol
399 R_ADDRPOWER, // relocation for loading 31-bit address using addis and addi/ld/st for Power
401 R_CALL, // relocation for direct PC-relative call
402 R_CALLARM, // relocation for ARM direct call
403 R_CALLIND, // marker for indirect call (no actual relocating necessary)
404 R_CALLPOWER, // relocation for Power direct call
408 R_TLS_LE, // TLS local exec offset from TLS segment register
409 R_TLS_IE, // TLS initial exec offset from TLS base pointer
415 R_POWER_TOC, // ELF R_PPC64_TOC16*
421 RV_NONE, // identity variant
422 RV_POWER_LO, // x & 0xFFFF
423 RV_POWER_HI, // x >> 16
424 RV_POWER_HA, // (x + 0x8000) >> 16
425 RV_POWER_DS, // x & 0xFFFC, check x&0x3 == 0
427 RV_CHECK_OVERFLOW = 1<<8, // check overflow flag
428 RV_TYPE_MASK = (RV_CHECK_OVERFLOW - 1),
470 char *objref; // object where we found the reference
471 char *srcref; // src file where we found the reference
472 char *file; // object file
473 char *pkg; // import path
503 // for(pciterinit(ctxt, &it, &pcd); !it.done; pciternext(&it)) { it.value holds in [it.pc, it.nextpc) }
516 void pciterinit(Link*, Pciter*, Pcdata*);
517 void pciternext(Pciter*);
519 // symbol version, incremented each time a file is loaded.
520 // version==1 is reserved for savehist.
526 // Link holds the context for writing object code from a compiler
527 // to be linker input or for reading that input into the linker.
530 int32 thechar; // '5' (arm), '6' (amd64), etc.
531 char* thestring; // full name of architecture ("arm", "amd64", ..)
532 int32 goarm; // for arm only, GOARM setting
536 int32 (*ignore)(char*); // do not emit names satisfying this function
537 int32 debugasm; // -S flag in compiler
538 int32 debugline; // -L flag in compiler
539 int32 debughist; // -O flag in linker
540 int32 debugread; // -W flag in linker
541 int32 debugvlog; // -v flag in linker
542 int32 debugstack; // -K flag in linker
543 int32 debugzerostack; // -Z flag in linker
544 int32 debugdivmod; // -M flag in 5l
545 int32 debugfloat; // -F flag in 5l
546 int32 debugpcln; // -O flag in linker
547 int32 flag_shared; // -shared flag in linker
549 Biobuf* bso; // for -v flag
555 int32 enforce_data_order; // for use by assembler
557 // hash table of all symbols
558 LSym* hash[LINKHASH];
575 LSym* symmorestack[2];
584 int repn; // for nacl
585 int lock; // for nacl
593 // for reading input files (during linker)
602 void (*diag)(char*, ...);
616 LittleEndian = 0x04030201,
617 BigEndian = 0x01020304,
620 // LinkArch is the definition of a single architecture.
623 char* name; // "arm", "amd64", and so on
624 int thechar; // '5', '6', and so on
625 int32 endian; // LittleEndian or BigEndian
627 void (*preprocess)(Link*, LSym*);
628 void (*assemble)(Link*, LSym*);
629 void (*follow)(Link*, LSym*);
630 void (*progedit)(Link*, Prog*);
637 /* executable header types */
660 extern uchar fnuxi8[8];
661 extern uchar fnuxi4[4];
662 extern uchar inuxi1[1];
663 extern uchar inuxi2[2];
664 extern uchar inuxi4[4];
665 extern uchar inuxi8[8];
668 void span5(Link *ctxt, LSym *s);
669 int chipfloat5(Link *ctxt, float64 e);
670 int chipzero5(Link *ctxt, float64 e);
673 void span6(Link *ctxt, LSym *s);
676 void span8(Link *ctxt, LSym *s);
679 void span9(Link *ctxt, LSym *s);
682 vlong addaddr(Link *ctxt, LSym *s, LSym *t);
683 vlong addaddrplus(Link *ctxt, LSym *s, LSym *t, vlong add);
684 vlong addaddrplus4(Link *ctxt, LSym *s, LSym *t, vlong add);
685 vlong addpcrelplus(Link *ctxt, LSym *s, LSym *t, vlong add);
686 Reloc* addrel(LSym *s);
687 vlong addsize(Link *ctxt, LSym *s, LSym *t);
688 vlong adduint16(Link *ctxt, LSym *s, uint16 v);
689 vlong adduint32(Link *ctxt, LSym *s, uint32 v);
690 vlong adduint64(Link *ctxt, LSym *s, uint64 v);
691 vlong adduint8(Link *ctxt, LSym *s, uint8 v);
692 vlong adduintxx(Link *ctxt, LSym *s, uint64 v, int wid);
693 void mangle(char *file);
694 void savedata(Link *ctxt, LSym *s, Prog *p, char *pn);
695 void savedata1(Link *ctxt, LSym *s, Prog *p, char *pn, int enforce_order);
696 vlong setaddr(Link *ctxt, LSym *s, vlong off, LSym *t);
697 vlong setaddrplus(Link *ctxt, LSym *s, vlong off, LSym *t, vlong add);
698 vlong setuint16(Link *ctxt, LSym *s, vlong r, uint16 v);
699 vlong setuint32(Link *ctxt, LSym *s, vlong r, uint32 v);
700 vlong setuint64(Link *ctxt, LSym *s, vlong r, uint64 v);
701 vlong setuint8(Link *ctxt, LSym *s, vlong r, uint8 v);
702 vlong setuintxx(Link *ctxt, LSym *s, vlong off, uint64 v, vlong wid);
703 void symgrow(Link *ctxt, LSym *s, vlong siz);
706 void double2ieee(uint64 *ieee, double native);
707 void* emallocz(long n);
708 void* erealloc(void *p, long n);
709 char* estrdup(char *p);
710 char* expandpkg(char *t0, char *pkg);
711 void linksetexp(void);
712 char* expstring(void);
714 extern int fieldtrack_enabled;
715 extern int framepointer_enabled;
718 void addhist(Link *ctxt, int32 line, int type);
719 void addlib(Link *ctxt, char *src, char *obj, char *path);
720 void addlibpath(Link *ctxt, char *srcref, char *objref, char *file, char *pkg);
721 void collapsefrog(Link *ctxt, LSym *s);
722 void copyhistfrog(Link *ctxt, char *buf, int nbuf);
723 int find1(int32 l, int c);
724 void linkgetline(Link *ctxt, int32 line, LSym **f, int32 *l);
725 void histtoauto(Link *ctxt);
727 void nuxiinit(LinkArch*);
728 void savehist(Link *ctxt, int32 line, int32 off);
729 Prog* copyp(Link*, Prog*);
730 Prog* appendp(Link*, Prog*);
731 vlong atolwhex(char*);
734 void listinit5(void);
735 void listinit6(void);
736 void listinit8(void);
737 void listinit9(void);
740 int linklinefmt(Link *ctxt, Fmt *fp);
741 void linklinehist(Link *ctxt, int lineno, char *f, int offset);
742 Plist* linknewplist(Link *ctxt);
743 void linkprfile(Link *ctxt, int32 l);
746 void ldobjfile(Link *ctxt, Biobuf *b, char *pkg, int64 len, char *path);
747 void writeobj(Link *ctxt, Biobuf *b);
750 Prog* brchain(Link *ctxt, Prog *p);
751 Prog* brloop(Link *ctxt, Prog *p);
752 void linkpatch(Link *ctxt, LSym *sym);
755 void linkpcln(Link*, LSym*);
758 LSym* linklookup(Link *ctxt, char *name, int v);
759 Link* linknew(LinkArch*);
760 LSym* linknewsym(Link *ctxt, char *symb, int v);
761 LSym* linkrlookup(Link *ctxt, char *name, int v);
762 int linksymfmt(Fmt *f);
766 extern char* anames5[];
767 extern char* anames6[];
768 extern char* anames8[];
769 extern char* anames9[];
771 extern char* cnames5[];
772 extern char* cnames9[];
774 extern char* dnames5[];
775 extern char* dnames6[];
776 extern char* dnames8[];
777 extern char* dnames9[];
779 extern LinkArch link386;
780 extern LinkArch linkamd64;
781 extern LinkArch linkamd64p32;
782 extern LinkArch linkarm;
783 extern LinkArch linkppc64;
784 extern LinkArch linkppc64le;
786 extern int linkbasepointer;
787 extern void linksetexp(void);
789 #pragma varargck type "A" int
790 #pragma varargck type "E" uint
791 #pragma varargck type "D" Addr*
792 #pragma varargck type "lD" Addr*
793 #pragma varargck type "P" Prog*
794 #pragma varargck type "R" int
795 #pragma varargck type "^" int // for 5l/9l, C_* classes (liblink internal)
797 // TODO(ality): remove this workaround.
798 // It's here because Pconv in liblink/list?.c references %L.
799 #pragma varargck type "L" int32