]> Cypherpunks.ru repositories - gostls13.git/blob - include/link.h
[dev.cc] cmd/go, liblink: turn off verification against Go code
[gostls13.git] / include / link.h
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
3 //
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.
12 //
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:
19 //
20 // The above copyright notice and this permission notice shall be included in
21 // all copies or substantial portions of the Software.
22 //
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
29 // THE SOFTWARE.
30
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;
41
42 typedef struct  Pcln    Pcln;
43 typedef struct  Pcdata  Pcdata;
44 typedef struct  Pciter  Pciter;
45
46 // prevent incompatible type signatures between liblink and 8l on Plan 9
47 #pragma incomplete struct Node
48
49 struct  Addr
50 {
51         vlong   offset;
52
53         union
54         {
55                 char    sval[8];
56                 float64 dval;
57                 Prog*   branch; // for 5g, 6g, 8g, 9g
58         } u;
59
60         LSym*   sym;
61         LSym*   gotype;
62         short   type;
63         uint8   index;
64         int8    scale;
65         int8    reg;    // for 5l, 9l; GPRs and FPRs both start at 0
66         int8    name; // for 5l, 9l
67         int8    class;  // for 5l, 9l
68         uint8   etype; // for 5g, 6g, 8g
69         int32   offset2;        // for 5l, 8l
70         struct Node*    node; // for 5g, 6g, 8g
71         int64   width; // for 5g, 6g, 8g
72 };
73
74 struct  Reloc
75 {
76         int32   off;
77         uchar   siz;
78         uchar   done;
79         int32   type;
80         int32   variant; // RV_*: variant on computed value
81         int64   add;
82         int64   xadd;
83         LSym*   sym;
84         LSym*   xsym;
85 };
86
87 struct  Prog
88 {
89         vlong   pc;
90         int32   lineno;
91         Prog*   link;
92         short   as;
93         uchar   scond; // arm only; condition codes
94
95         // operands
96         Addr    from;
97         uchar   reg; // arm, ppc64 only (e.g., ADD from, reg, to);
98                      // starts at 0 for both GPRs and FPRs;
99                      // also used for ADATA width on arm, ppc64
100         Addr    from3; // ppc64 only (e.g., RLWM/FMADD from, reg, from3, to)
101         Addr    to;
102         
103         // for 5g, 6g, 8g internal use
104         void*   opt;
105
106         // for 5l, 6l, 8l internal use
107         Prog*   forwd;
108         Prog*   pcond;
109         Prog*   comefrom;       // 6l, 8l
110         Prog*   pcrel;  // 5l
111         int32   spadj;
112         uint16  mark;
113         uint16  optab;  // 5l, 9l
114         uchar   back;   // 6l, 8l
115         uchar   ft;     /* 6l, 8l oclass cache */
116         uchar   tt;     // 6l, 8l
117         uchar   isize;  // 6l, 8l
118         uchar   printed;
119
120         char    width;  /* fake for DATA */
121         char    mode;   /* 16, 32, or 64 in 6l, 8l; internal use in 5g, 6g, 8g */
122         
123         /*c2go uchar TEXTFLAG; */
124 };
125
126 // prevent incompatible type signatures between liblink and 8l on Plan 9
127 #pragma incomplete struct Section
128
129 struct  LSym
130 {
131         char*   name;
132         char*   extname;        // name used in external object files
133         short   type;
134         short   version;
135         uchar   dupok;
136         uchar   cfunc;
137         uchar   external;
138         uchar   nosplit;
139         uchar   reachable;
140         uchar   cgoexport;
141         uchar   special;
142         uchar   stkcheck;
143         uchar   hide;
144         uchar   leaf;   // arm only
145         uchar   fnptr;  // arm only
146         uchar   localentry;     // ppc64: instrs between global & local entry
147         uchar   seenglobl;
148         uchar   onlist; // on the textp or datap lists
149         uchar   printed;
150         int16   symid;  // for writing .5/.6/.8 files
151         int32   dynid;
152         int32   sig;
153         int32   plt;
154         int32   got;
155         int32   align;  // if non-zero, required alignment in bytes
156         int32   elfsym;
157         int32   args;   // size of stack frame incoming arguments area
158         int32   locals; // size of stack frame locals area (arm only?)
159         vlong   value;
160         vlong   size;
161         LSym*   hash;   // in hash table
162         LSym*   allsym; // in all symbol list
163         LSym*   next;   // in text or data list
164         LSym*   sub;    // in SSUB list
165         LSym*   outer;  // container of sub
166         LSym*   gotype;
167         LSym*   reachparent;
168         LSym*   queue;
169         char*   file;
170         char*   dynimplib;
171         char*   dynimpvers;
172         struct Section* sect;
173         
174         // STEXT
175         Auto*   autom;
176         Prog*   text;
177         Prog*   etext;
178         Pcln*   pcln;
179
180         // SDATA, SBSS
181         uchar*  p;
182         int     np;
183         int32   maxp;
184         Reloc*  r;
185         int32   nr;
186         int32   maxr;
187 };
188
189 // LSym.type
190 enum
191 {
192         Sxxx,
193
194         /* order here is order in output file */
195         /* readonly, executable */
196         STEXT,
197         SELFRXSECT,
198         
199         /* readonly, non-executable */
200         STYPE,
201         SSTRING,
202         SGOSTRING,
203         SGOFUNC,
204         SRODATA,
205         SFUNCTAB,
206         STYPELINK,
207         SSYMTAB, // TODO: move to unmapped section
208         SPCLNTAB,
209         SELFROSECT,
210         
211         /* writable, non-executable */
212         SMACHOPLT,
213         SELFSECT,
214         SMACHO, /* Mach-O __nl_symbol_ptr */
215         SMACHOGOT,
216         SWINDOWS,
217         SELFGOT,        /* also .toc in ppc64 ABI */
218         SNOPTRDATA,
219         SINITARR,
220         SDATA,
221         SBSS,
222         SNOPTRBSS,
223         STLSBSS,
224
225         /* not mapped */
226         SXREF,
227         SMACHOSYMSTR,
228         SMACHOSYMTAB,
229         SMACHOINDIRECTPLT,
230         SMACHOINDIRECTGOT,
231         SFILE,
232         SFILEPATH,
233         SCONST,
234         SDYNIMPORT,
235         SHOSTOBJ,
236
237         SSUB = 1<<8,    /* sub-symbol, linked from parent via ->sub list */
238         SMASK = SSUB - 1,
239         SHIDDEN = 1<<9, // hidden or local symbol
240 };
241
242 // Reloc.type
243 enum
244 {
245         R_ADDR = 1,
246         R_ADDRPOWER, // relocation for loading 31-bit address using addis and addi/ld/st for Power
247         R_SIZE,
248         R_CALL, // relocation for direct PC-relative call
249         R_CALLARM, // relocation for ARM direct call
250         R_CALLIND, // marker for indirect call (no actual relocating necessary)
251         R_CALLPOWER, // relocation for Power direct call
252         R_CONST,
253         R_PCREL,
254         R_TLS,
255         R_TLS_LE, // TLS local exec offset from TLS segment register
256         R_TLS_IE, // TLS initial exec offset from TLS base pointer
257         R_GOTOFF,
258         R_PLT0,
259         R_PLT1,
260         R_PLT2,
261         R_USEFIELD,
262         R_POWER_TOC,            // ELF R_PPC64_TOC16*
263 };
264
265 // Reloc.variant
266 enum
267 {
268         RV_NONE,                // identity variant
269         RV_POWER_LO,            // x & 0xFFFF
270         RV_POWER_HI,            // x >> 16
271         RV_POWER_HA,            // (x + 0x8000) >> 16
272         RV_POWER_DS,            // x & 0xFFFC, check x&0x3 == 0
273
274         RV_CHECK_OVERFLOW = 1<<8,       // check overflow flag
275         RV_TYPE_MASK = (RV_CHECK_OVERFLOW - 1),
276 };
277
278 // Auto.type
279 enum
280 {
281         A_AUTO = 1,
282         A_PARAM,
283 };
284
285 struct  Auto
286 {
287         LSym*   asym;
288         Auto*   link;
289         int32   aoffset;
290         int16   type;
291         LSym*   gotype;
292 };
293
294 enum
295 {
296         LINKHASH = 100003,
297 };
298
299 struct  Hist
300 {
301         Hist*   link;
302         char*   name;
303         int32   line;
304         int32   offset;
305         uchar   printed;
306 };
307
308 struct  Plist
309 {
310         LSym*   name;
311         Prog*   firstpc;
312         int     recur;
313         Plist*  link;
314 };
315
316 struct  Library
317 {
318         char *objref;   // object where we found the reference
319         char *srcref;   // src file where we found the reference
320         char *file;     // object file
321         char *pkg;      // import path
322 };
323
324 struct Pcdata
325 {
326         uchar *p;
327         int n;
328         int m;
329 };
330
331 struct Pcln
332 {
333         Pcdata pcsp;
334         Pcdata pcfile;
335         Pcdata pcline;
336         Pcdata *pcdata;
337         int npcdata;
338         LSym **funcdata;
339         int64 *funcdataoff;
340         int nfuncdata;
341         
342         LSym **file;
343         int nfile;
344         int mfile;
345
346         LSym *lastfile;
347         int lastindex;
348 };
349
350 // Pcdata iterator.
351 //      for(pciterinit(ctxt, &it, &pcd); !it.done; pciternext(&it)) { it.value holds in [it.pc, it.nextpc) }
352 struct Pciter
353 {
354         Pcdata d;
355         uchar *p;
356         uint32 pc;
357         uint32 nextpc;
358         uint32 pcscale;
359         int32 value;
360         int start;
361         int done;
362 };
363
364 void    pciterinit(Link*, Pciter*, Pcdata*);
365 void    pciternext(Pciter*);
366
367 // symbol version, incremented each time a file is loaded.
368 // version==1 is reserved for savehist.
369 enum
370 {
371         HistVersion = 1,
372 };
373
374 // Link holds the context for writing object code from a compiler
375 // to be linker input or for reading that input into the linker.
376 struct  Link
377 {
378         int32   thechar; // '5' (arm), '6' (amd64), etc.
379         char*   thestring; // full name of architecture ("arm", "amd64", ..)
380         int32   goarm; // for arm only, GOARM setting
381         int     headtype;
382
383         LinkArch*       arch;
384         int32   (*ignore)(char*);       // do not emit names satisfying this function
385         int32   debugasm;       // -S flag in compiler
386         int32   debugline;      // -L flag in compiler
387         int32   debughist;      // -O flag in linker
388         int32   debugread;      // -W flag in linker
389         int32   debugvlog;      // -v flag in linker
390         int32   debugstack;     // -K flag in linker
391         int32   debugzerostack; // -Z flag in linker
392         int32   debugdivmod;    // -M flag in 5l
393         int32   debugfloat;     // -F flag in 5l
394         int32   debugpcln;      // -O flag in linker
395         int32   flag_shared;    // -shared flag in linker
396         int32   iself;
397         Biobuf* bso;    // for -v flag
398         char*   pathname;
399         int32   windows;
400         char*   trimpath;
401         char*   goroot;
402         char*   goroot_final;
403         int32   enforce_data_order;     // for use by assembler
404
405         // hash table of all symbols
406         LSym*   hash[LINKHASH];
407         LSym*   allsym;
408         int32   nsymbol;
409
410         // file-line history
411         Hist*   hist;
412         Hist*   ehist;
413         
414         // all programs
415         Plist*  plist;
416         Plist*  plast;
417         
418         // code generation
419         LSym*   sym_div;
420         LSym*   sym_divu;
421         LSym*   sym_mod;
422         LSym*   sym_modu;
423         LSym*   symmorestack[2];
424         LSym*   tlsg;
425         LSym*   plan9privates;
426         Prog*   curp;
427         Prog*   printp;
428         Prog*   blitrl;
429         Prog*   elitrl;
430         int     rexflag;
431         int     rep; // for nacl
432         int     repn; // for nacl
433         int     lock; // for nacl
434         int     asmode;
435         uchar*  andptr;
436         uchar   and[100];
437         int64   instoffset;
438         int32   autosize;
439         int32   armsize;
440
441         // for reading input files (during linker)
442         vlong   pc;
443         char**  libdir;
444         int32   nlibdir;
445         int32   maxlibdir;
446         Library*        library;
447         int     libraryp;
448         int     nlibrary;
449         int     tlsoffset;
450         void    (*diag)(char*, ...);
451         int     mode;
452         Auto*   curauto;
453         Auto*   curhist;
454         LSym*   cursym;
455         int     version;
456         LSym*   textp;
457         LSym*   etextp;
458         int32   histdepth;
459         int32   nhistfile;
460         LSym*   filesyms;
461 };
462
463 enum {
464         LittleEndian = 0x04030201,
465         BigEndian = 0x01020304,
466 };
467
468 // LinkArch is the definition of a single architecture.
469 struct LinkArch
470 {
471         char*   name; // "arm", "amd64", and so on
472         int     thechar;        // '5', '6', and so on
473         int32   endian; // LittleEndian or BigEndian
474
475         void    (*addstacksplit)(Link*, LSym*);
476         void    (*assemble)(Link*, LSym*);
477         int     (*datasize)(Prog*);
478         void    (*follow)(Link*, LSym*);
479         int     (*iscall)(Prog*);
480         int     (*isdata)(Prog*);
481         Prog*   (*prg)(void);
482         void    (*progedit)(Link*, Prog*);
483         void    (*settextflag)(Prog*, int);
484         int     (*symtype)(Addr*);
485         int     (*textflag)(Prog*);
486
487         int     minlc;
488         int     ptrsize;
489         int     regsize;
490         
491         // TODO: Give these the same values on all systems.
492         int     D_ADDR;
493         int     D_AUTO;
494         int     D_BRANCH;
495         int     D_CONST;
496         int     D_EXTERN;
497         int     D_FCONST;
498         int     D_NONE;
499         int     D_PARAM;
500         int     D_SCONST;
501         int     D_STATIC;
502         int     D_OREG;
503
504         int     ACALL;
505         int     ADATA;
506         int     AEND;
507         int     AFUNCDATA;
508         int     AGLOBL;
509         int     AJMP;
510         int     ANOP;
511         int     APCDATA;
512         int     ARET;
513         int     ATEXT;
514         int     ATYPE;
515         int     AUSEFIELD;
516 };
517
518 /* executable header types */
519 enum {
520         Hunknown = 0,
521         Hdarwin,
522         Hdragonfly,
523         Helf,
524         Hfreebsd,
525         Hlinux,
526         Hnacl,
527         Hnetbsd,
528         Hopenbsd,
529         Hplan9,
530         Hsolaris,
531         Hwindows,
532 };
533
534 enum
535 {
536         LinkAuto = 0,
537         LinkInternal,
538         LinkExternal,
539 };
540
541 extern  uchar   fnuxi8[8];
542 extern  uchar   fnuxi4[4];
543 extern  uchar   inuxi1[1];
544 extern  uchar   inuxi2[2];
545 extern  uchar   inuxi4[4];
546 extern  uchar   inuxi8[8];
547
548 // asm5.c
549 void    span5(Link *ctxt, LSym *s);
550 int     chipfloat5(Link *ctxt, float64 e);
551 int     chipzero5(Link *ctxt, float64 e);
552
553 // asm6.c
554 void    span6(Link *ctxt, LSym *s);
555
556 // asm8.c
557 void    span8(Link *ctxt, LSym *s);
558
559 // asm9.c
560 void    span9(Link *ctxt, LSym *s);
561
562 // data.c
563 vlong   addaddr(Link *ctxt, LSym *s, LSym *t);
564 vlong   addaddrplus(Link *ctxt, LSym *s, LSym *t, vlong add);
565 vlong   addaddrplus4(Link *ctxt, LSym *s, LSym *t, vlong add);
566 vlong   addpcrelplus(Link *ctxt, LSym *s, LSym *t, vlong add);
567 Reloc*  addrel(LSym *s);
568 vlong   addsize(Link *ctxt, LSym *s, LSym *t);
569 vlong   adduint16(Link *ctxt, LSym *s, uint16 v);
570 vlong   adduint32(Link *ctxt, LSym *s, uint32 v);
571 vlong   adduint64(Link *ctxt, LSym *s, uint64 v);
572 vlong   adduint8(Link *ctxt, LSym *s, uint8 v);
573 vlong   adduintxx(Link *ctxt, LSym *s, uint64 v, int wid);
574 void    mangle(char *file);
575 void    savedata(Link *ctxt, LSym *s, Prog *p, char *pn);
576 void    savedata1(Link *ctxt, LSym *s, Prog *p, char *pn, int enforce_order);
577 vlong   setaddr(Link *ctxt, LSym *s, vlong off, LSym *t);
578 vlong   setaddrplus(Link *ctxt, LSym *s, vlong off, LSym *t, vlong add);
579 vlong   setuint16(Link *ctxt, LSym *s, vlong r, uint16 v);
580 vlong   setuint32(Link *ctxt, LSym *s, vlong r, uint32 v);
581 vlong   setuint64(Link *ctxt, LSym *s, vlong r, uint64 v);
582 vlong   setuint8(Link *ctxt, LSym *s, vlong r, uint8 v);
583 vlong   setuintxx(Link *ctxt, LSym *s, vlong off, uint64 v, vlong wid);
584 void    symgrow(Link *ctxt, LSym *s, vlong siz);
585
586 // go.c
587 void    double2ieee(uint64 *ieee, double native);
588 void*   emallocz(long n);
589 void*   erealloc(void *p, long n);
590 char*   estrdup(char *p);
591 char*   expandpkg(char *t0, char *pkg);
592
593 // ld.c
594 void    addhist(Link *ctxt, int32 line, int type);
595 void    addlib(Link *ctxt, char *src, char *obj, char *path);
596 void    addlibpath(Link *ctxt, char *srcref, char *objref, char *file, char *pkg);
597 void    collapsefrog(Link *ctxt, LSym *s);
598 void    copyhistfrog(Link *ctxt, char *buf, int nbuf);
599 int     find1(int32 l, int c);
600 void    linkgetline(Link *ctxt, int32 line, LSym **f, int32 *l);
601 void    histtoauto(Link *ctxt);
602 void    mkfwd(LSym*);
603 void    nuxiinit(LinkArch*);
604 void    savehist(Link *ctxt, int32 line, int32 off);
605 Prog*   copyp(Link*, Prog*);
606 Prog*   appendp(Link*, Prog*);
607 vlong   atolwhex(char*);
608
609 // list[5689].c
610 void    listinit5(void);
611 void    listinit6(void);
612 void    listinit8(void);
613 void    listinit9(void);
614
615 // obj.c
616 int     linklinefmt(Link *ctxt, Fmt *fp);
617 void    linklinehist(Link *ctxt, int lineno, char *f, int offset);
618 Plist*  linknewplist(Link *ctxt);
619 void    linkprfile(Link *ctxt, int32 l);
620
621 // objfile.c
622 void    ldobjfile(Link *ctxt, Biobuf *b, char *pkg, int64 len, char *path);
623 void    writeobj(Link *ctxt, Biobuf *b);
624
625 // pass.c
626 Prog*   brchain(Link *ctxt, Prog *p);
627 Prog*   brloop(Link *ctxt, Prog *p);
628 void    linkpatch(Link *ctxt, LSym *sym);
629
630 // pcln.c
631 void    linkpcln(Link*, LSym*);
632
633 // sym.c
634 LSym*   linklookup(Link *ctxt, char *name, int v);
635 Link*   linknew(LinkArch*);
636 LSym*   linknewsym(Link *ctxt, char *symb, int v);
637 LSym*   linkrlookup(Link *ctxt, char *name, int v);
638 int     linksymfmt(Fmt *f);
639 int     headtype(char*);
640 char*   headstr(int);
641
642 extern  char*   anames5[];
643 extern  char*   anames6[];
644 extern  char*   anames8[];
645 extern  char*   anames9[];
646
647 extern  char*   cnames5[];
648 extern  char*   cnames9[];
649
650 extern  char*   dnames5[];
651 extern  char*   dnames6[];
652 extern  char*   dnames8[];
653 extern  char*   dnames9[];
654
655 extern  LinkArch        link386;
656 extern  LinkArch        linkamd64;
657 extern  LinkArch        linkamd64p32;
658 extern  LinkArch        linkarm;
659 extern  LinkArch        linkppc64;
660 extern  LinkArch        linkppc64le;
661
662 #pragma varargck        type    "A"     int
663 #pragma varargck        type    "E"     uint
664 #pragma varargck        type    "D"     Addr*
665 #pragma varargck        type    "lD"    Addr*
666 #pragma varargck        type    "P"     Prog*
667 #pragma varargck        type    "R"     int
668 #pragma varargck        type    "^"     int // for 5l/9l, C_* classes (liblink internal)
669
670 // TODO(ality): remove this workaround.
671 //   It's here because Pconv in liblink/list?.c references %L.
672 #pragma varargck        type    "L"     int32