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