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