link.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. // Derived from Inferno utils/6l/l.h and related files.
  2. // https://bitbucket.org/inferno-os/inferno-os/src/master/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. package obj
  31. import (
  32. "bufio"
  33. "github.com/twitchyliquid64/golang-asm/dwarf"
  34. "github.com/twitchyliquid64/golang-asm/goobj"
  35. "github.com/twitchyliquid64/golang-asm/objabi"
  36. "github.com/twitchyliquid64/golang-asm/src"
  37. "github.com/twitchyliquid64/golang-asm/sys"
  38. "fmt"
  39. "sync"
  40. )
  41. // An Addr is an argument to an instruction.
  42. // The general forms and their encodings are:
  43. //
  44. // sym±offset(symkind)(reg)(index*scale)
  45. // Memory reference at address &sym(symkind) + offset + reg + index*scale.
  46. // Any of sym(symkind), ±offset, (reg), (index*scale), and *scale can be omitted.
  47. // If (reg) and *scale are both omitted, the resulting expression (index) is parsed as (reg).
  48. // To force a parsing as index*scale, write (index*1).
  49. // Encoding:
  50. // type = TYPE_MEM
  51. // name = symkind (NAME_AUTO, ...) or 0 (NAME_NONE)
  52. // sym = sym
  53. // offset = ±offset
  54. // reg = reg (REG_*)
  55. // index = index (REG_*)
  56. // scale = scale (1, 2, 4, 8)
  57. //
  58. // $<mem>
  59. // Effective address of memory reference <mem>, defined above.
  60. // Encoding: same as memory reference, but type = TYPE_ADDR.
  61. //
  62. // $<±integer value>
  63. // This is a special case of $<mem>, in which only ±offset is present.
  64. // It has a separate type for easy recognition.
  65. // Encoding:
  66. // type = TYPE_CONST
  67. // offset = ±integer value
  68. //
  69. // *<mem>
  70. // Indirect reference through memory reference <mem>, defined above.
  71. // Only used on x86 for CALL/JMP *sym(SB), which calls/jumps to a function
  72. // pointer stored in the data word sym(SB), not a function named sym(SB).
  73. // Encoding: same as above, but type = TYPE_INDIR.
  74. //
  75. // $*$<mem>
  76. // No longer used.
  77. // On machines with actual SB registers, $*$<mem> forced the
  78. // instruction encoding to use a full 32-bit constant, never a
  79. // reference relative to SB.
  80. //
  81. // $<floating point literal>
  82. // Floating point constant value.
  83. // Encoding:
  84. // type = TYPE_FCONST
  85. // val = floating point value
  86. //
  87. // $<string literal, up to 8 chars>
  88. // String literal value (raw bytes used for DATA instruction).
  89. // Encoding:
  90. // type = TYPE_SCONST
  91. // val = string
  92. //
  93. // <register name>
  94. // Any register: integer, floating point, control, segment, and so on.
  95. // If looking for specific register kind, must check type and reg value range.
  96. // Encoding:
  97. // type = TYPE_REG
  98. // reg = reg (REG_*)
  99. //
  100. // x(PC)
  101. // Encoding:
  102. // type = TYPE_BRANCH
  103. // val = Prog* reference OR ELSE offset = target pc (branch takes priority)
  104. //
  105. // $±x-±y
  106. // Final argument to TEXT, specifying local frame size x and argument size y.
  107. // In this form, x and y are integer literals only, not arbitrary expressions.
  108. // This avoids parsing ambiguities due to the use of - as a separator.
  109. // The ± are optional.
  110. // If the final argument to TEXT omits the -±y, the encoding should still
  111. // use TYPE_TEXTSIZE (not TYPE_CONST), with u.argsize = ArgsSizeUnknown.
  112. // Encoding:
  113. // type = TYPE_TEXTSIZE
  114. // offset = x
  115. // val = int32(y)
  116. //
  117. // reg<<shift, reg>>shift, reg->shift, reg@>shift
  118. // Shifted register value, for ARM and ARM64.
  119. // In this form, reg must be a register and shift can be a register or an integer constant.
  120. // Encoding:
  121. // type = TYPE_SHIFT
  122. // On ARM:
  123. // offset = (reg&15) | shifttype<<5 | count
  124. // shifttype = 0, 1, 2, 3 for <<, >>, ->, @>
  125. // count = (reg&15)<<8 | 1<<4 for a register shift count, (n&31)<<7 for an integer constant.
  126. // On ARM64:
  127. // offset = (reg&31)<<16 | shifttype<<22 | (count&63)<<10
  128. // shifttype = 0, 1, 2 for <<, >>, ->
  129. //
  130. // (reg, reg)
  131. // A destination register pair. When used as the last argument of an instruction,
  132. // this form makes clear that both registers are destinations.
  133. // Encoding:
  134. // type = TYPE_REGREG
  135. // reg = first register
  136. // offset = second register
  137. //
  138. // [reg, reg, reg-reg]
  139. // Register list for ARM, ARM64, 386/AMD64.
  140. // Encoding:
  141. // type = TYPE_REGLIST
  142. // On ARM:
  143. // offset = bit mask of registers in list; R0 is low bit.
  144. // On ARM64:
  145. // offset = register count (Q:size) | arrangement (opcode) | first register
  146. // On 386/AMD64:
  147. // reg = range low register
  148. // offset = 2 packed registers + kind tag (see x86.EncodeRegisterRange)
  149. //
  150. // reg, reg
  151. // Register pair for ARM.
  152. // TYPE_REGREG2
  153. //
  154. // (reg+reg)
  155. // Register pair for PPC64.
  156. // Encoding:
  157. // type = TYPE_MEM
  158. // reg = first register
  159. // index = second register
  160. // scale = 1
  161. //
  162. // reg.[US]XT[BHWX]
  163. // Register extension for ARM64
  164. // Encoding:
  165. // type = TYPE_REG
  166. // reg = REG_[US]XT[BHWX] + register + shift amount
  167. // offset = ((reg&31) << 16) | (exttype << 13) | (amount<<10)
  168. //
  169. // reg.<T>
  170. // Register arrangement for ARM64 SIMD register
  171. // e.g.: V1.S4, V2.S2, V7.D2, V2.H4, V6.B16
  172. // Encoding:
  173. // type = TYPE_REG
  174. // reg = REG_ARNG + register + arrangement
  175. //
  176. // reg.<T>[index]
  177. // Register element for ARM64
  178. // Encoding:
  179. // type = TYPE_REG
  180. // reg = REG_ELEM + register + arrangement
  181. // index = element index
  182. type Addr struct {
  183. Reg int16
  184. Index int16
  185. Scale int16 // Sometimes holds a register.
  186. Type AddrType
  187. Name AddrName
  188. Class int8
  189. Offset int64
  190. Sym *LSym
  191. // argument value:
  192. // for TYPE_SCONST, a string
  193. // for TYPE_FCONST, a float64
  194. // for TYPE_BRANCH, a *Prog (optional)
  195. // for TYPE_TEXTSIZE, an int32 (optional)
  196. Val interface{}
  197. }
  198. type AddrName int8
  199. const (
  200. NAME_NONE AddrName = iota
  201. NAME_EXTERN
  202. NAME_STATIC
  203. NAME_AUTO
  204. NAME_PARAM
  205. // A reference to name@GOT(SB) is a reference to the entry in the global offset
  206. // table for 'name'.
  207. NAME_GOTREF
  208. // Indicates that this is a reference to a TOC anchor.
  209. NAME_TOCREF
  210. )
  211. //go:generate stringer -type AddrType
  212. type AddrType uint8
  213. const (
  214. TYPE_NONE AddrType = iota
  215. TYPE_BRANCH
  216. TYPE_TEXTSIZE
  217. TYPE_MEM
  218. TYPE_CONST
  219. TYPE_FCONST
  220. TYPE_SCONST
  221. TYPE_REG
  222. TYPE_ADDR
  223. TYPE_SHIFT
  224. TYPE_REGREG
  225. TYPE_REGREG2
  226. TYPE_INDIR
  227. TYPE_REGLIST
  228. )
  229. func (a *Addr) Target() *Prog {
  230. if a.Type == TYPE_BRANCH && a.Val != nil {
  231. return a.Val.(*Prog)
  232. }
  233. return nil
  234. }
  235. func (a *Addr) SetTarget(t *Prog) {
  236. if a.Type != TYPE_BRANCH {
  237. panic("setting branch target when type is not TYPE_BRANCH")
  238. }
  239. a.Val = t
  240. }
  241. // Prog describes a single machine instruction.
  242. //
  243. // The general instruction form is:
  244. //
  245. // (1) As.Scond From [, ...RestArgs], To
  246. // (2) As.Scond From, Reg [, ...RestArgs], To, RegTo2
  247. //
  248. // where As is an opcode and the others are arguments:
  249. // From, Reg are sources, and To, RegTo2 are destinations.
  250. // RestArgs can hold additional sources and destinations.
  251. // Usually, not all arguments are present.
  252. // For example, MOVL R1, R2 encodes using only As=MOVL, From=R1, To=R2.
  253. // The Scond field holds additional condition bits for systems (like arm)
  254. // that have generalized conditional execution.
  255. // (2) form is present for compatibility with older code,
  256. // to avoid too much changes in a single swing.
  257. // (1) scheme is enough to express any kind of operand combination.
  258. //
  259. // Jump instructions use the To.Val field to point to the target *Prog,
  260. // which must be in the same linked list as the jump instruction.
  261. //
  262. // The Progs for a given function are arranged in a list linked through the Link field.
  263. //
  264. // Each Prog is charged to a specific source line in the debug information,
  265. // specified by Pos.Line().
  266. // Every Prog has a Ctxt field that defines its context.
  267. // For performance reasons, Progs usually are usually bulk allocated, cached, and reused;
  268. // those bulk allocators should always be used, rather than new(Prog).
  269. //
  270. // The other fields not yet mentioned are for use by the back ends and should
  271. // be left zeroed by creators of Prog lists.
  272. type Prog struct {
  273. Ctxt *Link // linker context
  274. Link *Prog // next Prog in linked list
  275. From Addr // first source operand
  276. RestArgs []Addr // can pack any operands that not fit into {Prog.From, Prog.To}
  277. To Addr // destination operand (second is RegTo2 below)
  278. Pool *Prog // constant pool entry, for arm,arm64 back ends
  279. Forwd *Prog // for x86 back end
  280. Rel *Prog // for x86, arm back ends
  281. Pc int64 // for back ends or assembler: virtual or actual program counter, depending on phase
  282. Pos src.XPos // source position of this instruction
  283. Spadj int32 // effect of instruction on stack pointer (increment or decrement amount)
  284. As As // assembler opcode
  285. Reg int16 // 2nd source operand
  286. RegTo2 int16 // 2nd destination operand
  287. Mark uint16 // bitmask of arch-specific items
  288. Optab uint16 // arch-specific opcode index
  289. Scond uint8 // bits that describe instruction suffixes (e.g. ARM conditions)
  290. Back uint8 // for x86 back end: backwards branch state
  291. Ft uint8 // for x86 back end: type index of Prog.From
  292. Tt uint8 // for x86 back end: type index of Prog.To
  293. Isize uint8 // for x86 back end: size of the instruction in bytes
  294. }
  295. // From3Type returns p.GetFrom3().Type, or TYPE_NONE when
  296. // p.GetFrom3() returns nil.
  297. //
  298. // Deprecated: for the same reasons as Prog.GetFrom3.
  299. func (p *Prog) From3Type() AddrType {
  300. if p.RestArgs == nil {
  301. return TYPE_NONE
  302. }
  303. return p.RestArgs[0].Type
  304. }
  305. // GetFrom3 returns second source operand (the first is Prog.From).
  306. // In combination with Prog.From and Prog.To it makes common 3 operand
  307. // case easier to use.
  308. //
  309. // Should be used only when RestArgs is set with SetFrom3.
  310. //
  311. // Deprecated: better use RestArgs directly or define backend-specific getters.
  312. // Introduced to simplify transition to []Addr.
  313. // Usage of this is discouraged due to fragility and lack of guarantees.
  314. func (p *Prog) GetFrom3() *Addr {
  315. if p.RestArgs == nil {
  316. return nil
  317. }
  318. return &p.RestArgs[0]
  319. }
  320. // SetFrom3 assigns []Addr{a} to p.RestArgs.
  321. // In pair with Prog.GetFrom3 it can help in emulation of Prog.From3.
  322. //
  323. // Deprecated: for the same reasons as Prog.GetFrom3.
  324. func (p *Prog) SetFrom3(a Addr) {
  325. p.RestArgs = []Addr{a}
  326. }
  327. // An As denotes an assembler opcode.
  328. // There are some portable opcodes, declared here in package obj,
  329. // that are common to all architectures.
  330. // However, the majority of opcodes are arch-specific
  331. // and are declared in their respective architecture's subpackage.
  332. type As int16
  333. // These are the portable opcodes.
  334. const (
  335. AXXX As = iota
  336. ACALL
  337. ADUFFCOPY
  338. ADUFFZERO
  339. AEND
  340. AFUNCDATA
  341. AJMP
  342. ANOP
  343. APCALIGN
  344. APCDATA
  345. ARET
  346. AGETCALLERPC
  347. ATEXT
  348. AUNDEF
  349. A_ARCHSPECIFIC
  350. )
  351. // Each architecture is allotted a distinct subspace of opcode values
  352. // for declaring its arch-specific opcodes.
  353. // Within this subspace, the first arch-specific opcode should be
  354. // at offset A_ARCHSPECIFIC.
  355. //
  356. // Subspaces are aligned to a power of two so opcodes can be masked
  357. // with AMask and used as compact array indices.
  358. const (
  359. ABase386 = (1 + iota) << 11
  360. ABaseARM
  361. ABaseAMD64
  362. ABasePPC64
  363. ABaseARM64
  364. ABaseMIPS
  365. ABaseRISCV
  366. ABaseS390X
  367. ABaseWasm
  368. AllowedOpCodes = 1 << 11 // The number of opcodes available for any given architecture.
  369. AMask = AllowedOpCodes - 1 // AND with this to use the opcode as an array index.
  370. )
  371. // An LSym is the sort of symbol that is written to an object file.
  372. // It represents Go symbols in a flat pkg+"."+name namespace.
  373. type LSym struct {
  374. Name string
  375. Type objabi.SymKind
  376. Attribute
  377. RefIdx int // Index of this symbol in the symbol reference list.
  378. Size int64
  379. Gotype *LSym
  380. P []byte
  381. R []Reloc
  382. Func *FuncInfo
  383. Pkg string
  384. PkgIdx int32
  385. SymIdx int32 // TODO: replace RefIdx
  386. }
  387. // A FuncInfo contains extra fields for STEXT symbols.
  388. type FuncInfo struct {
  389. Args int32
  390. Locals int32
  391. Align int32
  392. FuncID objabi.FuncID
  393. Text *Prog
  394. Autot map[*LSym]struct{}
  395. Pcln Pcln
  396. InlMarks []InlMark
  397. dwarfInfoSym *LSym
  398. dwarfLocSym *LSym
  399. dwarfRangesSym *LSym
  400. dwarfAbsFnSym *LSym
  401. dwarfDebugLinesSym *LSym
  402. GCArgs *LSym
  403. GCLocals *LSym
  404. GCRegs *LSym // Only if !go115ReduceLiveness
  405. StackObjects *LSym
  406. OpenCodedDeferInfo *LSym
  407. FuncInfoSym *LSym
  408. }
  409. type InlMark struct {
  410. // When unwinding from an instruction in an inlined body, mark
  411. // where we should unwind to.
  412. // id records the global inlining id of the inlined body.
  413. // p records the location of an instruction in the parent (inliner) frame.
  414. p *Prog
  415. id int32
  416. }
  417. // Mark p as the instruction to set as the pc when
  418. // "unwinding" the inlining global frame id. Usually it should be
  419. // instruction with a file:line at the callsite, and occur
  420. // just before the body of the inlined function.
  421. func (fi *FuncInfo) AddInlMark(p *Prog, id int32) {
  422. fi.InlMarks = append(fi.InlMarks, InlMark{p: p, id: id})
  423. }
  424. // Record the type symbol for an auto variable so that the linker
  425. // an emit DWARF type information for the type.
  426. func (fi *FuncInfo) RecordAutoType(gotype *LSym) {
  427. if fi.Autot == nil {
  428. fi.Autot = make(map[*LSym]struct{})
  429. }
  430. fi.Autot[gotype] = struct{}{}
  431. }
  432. //go:generate stringer -type ABI
  433. // ABI is the calling convention of a text symbol.
  434. type ABI uint8
  435. const (
  436. // ABI0 is the stable stack-based ABI. It's important that the
  437. // value of this is "0": we can't distinguish between
  438. // references to data and ABI0 text symbols in assembly code,
  439. // and hence this doesn't distinguish between symbols without
  440. // an ABI and text symbols with ABI0.
  441. ABI0 ABI = iota
  442. // ABIInternal is the internal ABI that may change between Go
  443. // versions. All Go functions use the internal ABI and the
  444. // compiler generates wrappers for calls to and from other
  445. // ABIs.
  446. ABIInternal
  447. ABICount
  448. )
  449. // Attribute is a set of symbol attributes.
  450. type Attribute uint32
  451. const (
  452. AttrDuplicateOK Attribute = 1 << iota
  453. AttrCFunc
  454. AttrNoSplit
  455. AttrLeaf
  456. AttrWrapper
  457. AttrNeedCtxt
  458. AttrNoFrame
  459. AttrOnList
  460. AttrStatic
  461. // MakeTypelink means that the type should have an entry in the typelink table.
  462. AttrMakeTypelink
  463. // ReflectMethod means the function may call reflect.Type.Method or
  464. // reflect.Type.MethodByName. Matching is imprecise (as reflect.Type
  465. // can be used through a custom interface), so ReflectMethod may be
  466. // set in some cases when the reflect package is not called.
  467. //
  468. // Used by the linker to determine what methods can be pruned.
  469. AttrReflectMethod
  470. // Local means make the symbol local even when compiling Go code to reference Go
  471. // symbols in other shared libraries, as in this mode symbols are global by
  472. // default. "local" here means in the sense of the dynamic linker, i.e. not
  473. // visible outside of the module (shared library or executable) that contains its
  474. // definition. (When not compiling to support Go shared libraries, all symbols are
  475. // local in this sense unless there is a cgo_export_* directive).
  476. AttrLocal
  477. // For function symbols; indicates that the specified function was the
  478. // target of an inline during compilation
  479. AttrWasInlined
  480. // TopFrame means that this function is an entry point and unwinders should not
  481. // keep unwinding beyond this frame.
  482. AttrTopFrame
  483. // Indexed indicates this symbol has been assigned with an index (when using the
  484. // new object file format).
  485. AttrIndexed
  486. // Only applied on type descriptor symbols, UsedInIface indicates this type is
  487. // converted to an interface.
  488. //
  489. // Used by the linker to determine what methods can be pruned.
  490. AttrUsedInIface
  491. // ContentAddressable indicates this is a content-addressable symbol.
  492. AttrContentAddressable
  493. // attrABIBase is the value at which the ABI is encoded in
  494. // Attribute. This must be last; all bits after this are
  495. // assumed to be an ABI value.
  496. //
  497. // MUST BE LAST since all bits above this comprise the ABI.
  498. attrABIBase
  499. )
  500. func (a Attribute) DuplicateOK() bool { return a&AttrDuplicateOK != 0 }
  501. func (a Attribute) MakeTypelink() bool { return a&AttrMakeTypelink != 0 }
  502. func (a Attribute) CFunc() bool { return a&AttrCFunc != 0 }
  503. func (a Attribute) NoSplit() bool { return a&AttrNoSplit != 0 }
  504. func (a Attribute) Leaf() bool { return a&AttrLeaf != 0 }
  505. func (a Attribute) OnList() bool { return a&AttrOnList != 0 }
  506. func (a Attribute) ReflectMethod() bool { return a&AttrReflectMethod != 0 }
  507. func (a Attribute) Local() bool { return a&AttrLocal != 0 }
  508. func (a Attribute) Wrapper() bool { return a&AttrWrapper != 0 }
  509. func (a Attribute) NeedCtxt() bool { return a&AttrNeedCtxt != 0 }
  510. func (a Attribute) NoFrame() bool { return a&AttrNoFrame != 0 }
  511. func (a Attribute) Static() bool { return a&AttrStatic != 0 }
  512. func (a Attribute) WasInlined() bool { return a&AttrWasInlined != 0 }
  513. func (a Attribute) TopFrame() bool { return a&AttrTopFrame != 0 }
  514. func (a Attribute) Indexed() bool { return a&AttrIndexed != 0 }
  515. func (a Attribute) UsedInIface() bool { return a&AttrUsedInIface != 0 }
  516. func (a Attribute) ContentAddressable() bool { return a&AttrContentAddressable != 0 }
  517. func (a *Attribute) Set(flag Attribute, value bool) {
  518. if value {
  519. *a |= flag
  520. } else {
  521. *a &^= flag
  522. }
  523. }
  524. func (a Attribute) ABI() ABI { return ABI(a / attrABIBase) }
  525. func (a *Attribute) SetABI(abi ABI) {
  526. const mask = 1 // Only one ABI bit for now.
  527. *a = (*a &^ (mask * attrABIBase)) | Attribute(abi)*attrABIBase
  528. }
  529. var textAttrStrings = [...]struct {
  530. bit Attribute
  531. s string
  532. }{
  533. {bit: AttrDuplicateOK, s: "DUPOK"},
  534. {bit: AttrMakeTypelink, s: ""},
  535. {bit: AttrCFunc, s: "CFUNC"},
  536. {bit: AttrNoSplit, s: "NOSPLIT"},
  537. {bit: AttrLeaf, s: "LEAF"},
  538. {bit: AttrOnList, s: ""},
  539. {bit: AttrReflectMethod, s: "REFLECTMETHOD"},
  540. {bit: AttrLocal, s: "LOCAL"},
  541. {bit: AttrWrapper, s: "WRAPPER"},
  542. {bit: AttrNeedCtxt, s: "NEEDCTXT"},
  543. {bit: AttrNoFrame, s: "NOFRAME"},
  544. {bit: AttrStatic, s: "STATIC"},
  545. {bit: AttrWasInlined, s: ""},
  546. {bit: AttrTopFrame, s: "TOPFRAME"},
  547. {bit: AttrIndexed, s: ""},
  548. {bit: AttrContentAddressable, s: ""},
  549. }
  550. // TextAttrString formats a for printing in as part of a TEXT prog.
  551. func (a Attribute) TextAttrString() string {
  552. var s string
  553. for _, x := range textAttrStrings {
  554. if a&x.bit != 0 {
  555. if x.s != "" {
  556. s += x.s + "|"
  557. }
  558. a &^= x.bit
  559. }
  560. }
  561. switch a.ABI() {
  562. case ABI0:
  563. case ABIInternal:
  564. s += "ABIInternal|"
  565. a.SetABI(0) // Clear ABI so we don't print below.
  566. }
  567. if a != 0 {
  568. s += fmt.Sprintf("UnknownAttribute(%d)|", a)
  569. }
  570. // Chop off trailing |, if present.
  571. if len(s) > 0 {
  572. s = s[:len(s)-1]
  573. }
  574. return s
  575. }
  576. func (s *LSym) String() string {
  577. return s.Name
  578. }
  579. // The compiler needs *LSym to be assignable to cmd/compile/internal/ssa.Sym.
  580. func (s *LSym) CanBeAnSSASym() {
  581. }
  582. type Pcln struct {
  583. Pcsp Pcdata
  584. Pcfile Pcdata
  585. Pcline Pcdata
  586. Pcinline Pcdata
  587. Pcdata []Pcdata
  588. Funcdata []*LSym
  589. Funcdataoff []int64
  590. UsedFiles map[goobj.CUFileIndex]struct{} // file indices used while generating pcfile
  591. InlTree InlTree // per-function inlining tree extracted from the global tree
  592. }
  593. type Reloc struct {
  594. Off int32
  595. Siz uint8
  596. Type objabi.RelocType
  597. Add int64
  598. Sym *LSym
  599. }
  600. type Auto struct {
  601. Asym *LSym
  602. Aoffset int32
  603. Name AddrName
  604. Gotype *LSym
  605. }
  606. type Pcdata struct {
  607. P []byte
  608. }
  609. // Link holds the context for writing object code from a compiler
  610. // to be linker input or for reading that input into the linker.
  611. type Link struct {
  612. Headtype objabi.HeadType
  613. Arch *LinkArch
  614. Debugasm int
  615. Debugvlog bool
  616. Debugpcln string
  617. Flag_shared bool
  618. Flag_dynlink bool
  619. Flag_linkshared bool
  620. Flag_optimize bool
  621. Flag_locationlists bool
  622. Retpoline bool // emit use of retpoline stubs for indirect jmp/call
  623. Bso *bufio.Writer
  624. Pathname string
  625. Pkgpath string // the current package's import path, "" if unknown
  626. hashmu sync.Mutex // protects hash, funchash
  627. hash map[string]*LSym // name -> sym mapping
  628. funchash map[string]*LSym // name -> sym mapping for ABIInternal syms
  629. statichash map[string]*LSym // name -> sym mapping for static syms
  630. PosTable src.PosTable
  631. InlTree InlTree // global inlining tree used by gc/inl.go
  632. DwFixups *DwarfFixupTable
  633. Imports []goobj.ImportedPkg
  634. DiagFunc func(string, ...interface{})
  635. DiagFlush func()
  636. DebugInfo func(fn *LSym, info *LSym, curfn interface{}) ([]dwarf.Scope, dwarf.InlCalls) // if non-nil, curfn is a *gc.Node
  637. GenAbstractFunc func(fn *LSym)
  638. Errors int
  639. InParallel bool // parallel backend phase in effect
  640. UseBASEntries bool // use Base Address Selection Entries in location lists and PC ranges
  641. IsAsm bool // is the source assembly language, which may contain surprising idioms (e.g., call tables)
  642. // state for writing objects
  643. Text []*LSym
  644. Data []*LSym
  645. // ABIAliases are text symbols that should be aliased to all
  646. // ABIs. These symbols may only be referenced and not defined
  647. // by this object, since the need for an alias may appear in a
  648. // different object than the definition. Hence, this
  649. // information can't be carried in the symbol definition.
  650. //
  651. // TODO(austin): Replace this with ABI wrappers once the ABIs
  652. // actually diverge.
  653. ABIAliases []*LSym
  654. // Constant symbols (e.g. $i64.*) are data symbols created late
  655. // in the concurrent phase. To ensure a deterministic order, we
  656. // add them to a separate list, sort at the end, and append it
  657. // to Data.
  658. constSyms []*LSym
  659. // pkgIdx maps package path to index. The index is used for
  660. // symbol reference in the object file.
  661. pkgIdx map[string]int32
  662. defs []*LSym // list of defined symbols in the current package
  663. hashed64defs []*LSym // list of defined short (64-bit or less) hashed (content-addressable) symbols
  664. hasheddefs []*LSym // list of defined hashed (content-addressable) symbols
  665. nonpkgdefs []*LSym // list of defined non-package symbols
  666. nonpkgrefs []*LSym // list of referenced non-package symbols
  667. Fingerprint goobj.FingerprintType // fingerprint of symbol indices, to catch index mismatch
  668. }
  669. func (ctxt *Link) Diag(format string, args ...interface{}) {
  670. ctxt.Errors++
  671. ctxt.DiagFunc(format, args...)
  672. }
  673. func (ctxt *Link) Logf(format string, args ...interface{}) {
  674. fmt.Fprintf(ctxt.Bso, format, args...)
  675. ctxt.Bso.Flush()
  676. }
  677. // The smallest possible offset from the hardware stack pointer to a local
  678. // variable on the stack. Architectures that use a link register save its value
  679. // on the stack in the function prologue and so always have a pointer between
  680. // the hardware stack pointer and the local variable area.
  681. func (ctxt *Link) FixedFrameSize() int64 {
  682. switch ctxt.Arch.Family {
  683. case sys.AMD64, sys.I386, sys.Wasm:
  684. return 0
  685. case sys.PPC64:
  686. // PIC code on ppc64le requires 32 bytes of stack, and it's easier to
  687. // just use that much stack always on ppc64x.
  688. return int64(4 * ctxt.Arch.PtrSize)
  689. default:
  690. return int64(ctxt.Arch.PtrSize)
  691. }
  692. }
  693. // LinkArch is the definition of a single architecture.
  694. type LinkArch struct {
  695. *sys.Arch
  696. Init func(*Link)
  697. Preprocess func(*Link, *LSym, ProgAlloc)
  698. Assemble func(*Link, *LSym, ProgAlloc)
  699. Progedit func(*Link, *Prog, ProgAlloc)
  700. UnaryDst map[As]bool // Instruction takes one operand, a destination.
  701. DWARFRegisters map[int16]int16
  702. }