1// Derived from Inferno utils/6l/l.h and related files.
2// https://bitbucket.org/inferno-os/inferno-os/src/default/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
31package objabi
32
33type RelocType int32
34
35//go:generate stringer -type=RelocType
36const (
37	R_ADDR RelocType = 1 + iota
38	// R_ADDRPOWER relocates a pair of "D-form" instructions (instructions with 16-bit
39	// immediates in the low half of the instruction word), usually addis followed by
40	// another add or a load, inserting the "high adjusted" 16 bits of the address of
41	// the referenced symbol into the immediate field of the first instruction and the
42	// low 16 bits into that of the second instruction.
43	R_ADDRPOWER
44	// R_ADDRARM64 relocates an adrp, add pair to compute the address of the
45	// referenced symbol.
46	R_ADDRARM64
47	// R_ADDRMIPS (only used on mips/mips64) resolves to the low 16 bits of an external
48	// address, by encoding it into the instruction.
49	R_ADDRMIPS
50	// R_ADDROFF resolves to a 32-bit offset from the beginning of the section
51	// holding the data being relocated to the referenced symbol.
52	R_ADDROFF
53	// R_WEAKADDROFF resolves just like R_ADDROFF but is a weak relocation.
54	// A weak relocation does not make the symbol it refers to reachable,
55	// and is only honored by the linker if the symbol is in some other way
56	// reachable.
57	R_WEAKADDROFF
58	R_SIZE
59	R_CALL
60	R_CALLARM
61	R_CALLARM64
62	R_CALLIND
63	R_CALLPOWER
64	// R_CALLMIPS (only used on mips64) resolves to non-PC-relative target address
65	// of a CALL (JAL) instruction, by encoding the address into the instruction.
66	R_CALLMIPS
67	R_CONST
68	R_PCREL
69	// R_TLS_LE, used on 386, amd64, and ARM, resolves to the offset of the
70	// thread-local symbol from the thread local base and is used to implement the
71	// "local exec" model for tls access (r.Sym is not set on intel platforms but is
72	// set to a TLS symbol -- runtime.tlsg -- in the linker when externally linking).
73	R_TLS_LE
74	// R_TLS_IE, used 386, amd64, and ARM resolves to the PC-relative offset to a GOT
75	// slot containing the offset from the thread-local symbol from the thread local
76	// base and is used to implemented the "initial exec" model for tls access (r.Sym
77	// is not set on intel platforms but is set to a TLS symbol -- runtime.tlsg -- in
78	// the linker when externally linking).
79	R_TLS_IE
80	R_GOTOFF
81	R_PLT0
82	R_PLT1
83	R_PLT2
84	R_USEFIELD
85	// R_USETYPE resolves to an *rtype, but no relocation is created. The
86	// linker uses this as a signal that the pointed-to type information
87	// should be linked into the final binary, even if there are no other
88	// direct references. (This is used for types reachable by reflection.)
89	R_USETYPE
90	// R_METHODOFF resolves to a 32-bit offset from the beginning of the section
91	// holding the data being relocated to the referenced symbol.
92	// It is a variant of R_ADDROFF used when linking from the uncommonType of a
93	// *rtype, and may be set to zero by the linker if it determines the method
94	// text is unreachable by the linked program.
95	R_METHODOFF
96	R_POWER_TOC
97	R_GOTPCREL
98	// R_JMPMIPS (only used on mips64) resolves to non-PC-relative target address
99	// of a JMP instruction, by encoding the address into the instruction.
100	// The stack nosplit check ignores this since it is not a function call.
101	R_JMPMIPS
102
103	// R_DWARFSECREF resolves to the offset of the symbol from its section.
104	// Target of relocation must be size 4 (in current implementation).
105	R_DWARFSECREF
106
107	// R_DWARFFILEREF resolves to an index into the DWARF .debug_line
108	// file table for the specified file symbol. Must be applied to an
109	// attribute of form DW_FORM_data4.
110	R_DWARFFILEREF
111
112	// Platform dependent relocations. Architectures with fixed width instructions
113	// have the inherent issue that a 32-bit (or 64-bit!) displacement cannot be
114	// stuffed into a 32-bit instruction, so an address needs to be spread across
115	// several instructions, and in turn this requires a sequence of relocations, each
116	// updating a part of an instruction. This leads to relocation codes that are
117	// inherently processor specific.
118
119	// Arm64.
120
121	// Set a MOV[NZ] immediate field to bits [15:0] of the offset from the thread
122	// local base to the thread local variable defined by the referenced (thread
123	// local) symbol. Error if the offset does not fit into 16 bits.
124	R_ARM64_TLS_LE
125
126	// Relocates an ADRP; LD64 instruction sequence to load the offset between
127	// the thread local base and the thread local variable defined by the
128	// referenced (thread local) symbol from the GOT.
129	R_ARM64_TLS_IE
130
131	// R_ARM64_GOTPCREL relocates an adrp, ld64 pair to compute the address of the GOT
132	// slot of the referenced symbol.
133	R_ARM64_GOTPCREL
134
135	// PPC64.
136
137	// R_POWER_TLS_LE is used to implement the "local exec" model for tls
138	// access. It resolves to the offset of the thread-local symbol from the
139	// thread pointer (R13) and inserts this value into the low 16 bits of an
140	// instruction word.
141	R_POWER_TLS_LE
142
143	// R_POWER_TLS_IE is used to implement the "initial exec" model for tls access. It
144	// relocates a D-form, DS-form instruction sequence like R_ADDRPOWER_DS. It
145	// inserts to the offset of GOT slot for the thread-local symbol from the TOC (the
146	// GOT slot is filled by the dynamic linker with the offset of the thread-local
147	// symbol from the thread pointer (R13)).
148	R_POWER_TLS_IE
149
150	// R_POWER_TLS marks an X-form instruction such as "MOVD 0(R13)(R31*1), g" as
151	// accessing a particular thread-local symbol. It does not affect code generation
152	// but is used by the system linker when relaxing "initial exec" model code to
153	// "local exec" model code.
154	R_POWER_TLS
155
156	// R_ADDRPOWER_DS is similar to R_ADDRPOWER above, but assumes the second
157	// instruction is a "DS-form" instruction, which has an immediate field occupying
158	// bits [15:2] of the instruction word. Bits [15:2] of the address of the
159	// relocated symbol are inserted into this field; it is an error if the last two
160	// bits of the address are not 0.
161	R_ADDRPOWER_DS
162
163	// R_ADDRPOWER_PCREL relocates a D-form, DS-form instruction sequence like
164	// R_ADDRPOWER_DS but inserts the offset of the GOT slot for the referenced symbol
165	// from the TOC rather than the symbol's address.
166	R_ADDRPOWER_GOT
167
168	// R_ADDRPOWER_PCREL relocates two D-form instructions like R_ADDRPOWER, but
169	// inserts the displacement from the place being relocated to the address of the
170	// the relocated symbol instead of just its address.
171	R_ADDRPOWER_PCREL
172
173	// R_ADDRPOWER_TOCREL relocates two D-form instructions like R_ADDRPOWER, but
174	// inserts the offset from the TOC to the address of the relocated symbol
175	// rather than the symbol's address.
176	R_ADDRPOWER_TOCREL
177
178	// R_ADDRPOWER_TOCREL relocates a D-form, DS-form instruction sequence like
179	// R_ADDRPOWER_DS but inserts the offset from the TOC to the address of the the
180	// relocated symbol rather than the symbol's address.
181	R_ADDRPOWER_TOCREL_DS
182
183	// R_PCRELDBL relocates s390x 2-byte aligned PC-relative addresses.
184	// TODO(mundaym): remove once variants can be serialized - see issue 14218.
185	R_PCRELDBL
186
187	// R_ADDRMIPSU (only used on mips/mips64) resolves to the sign-adjusted "upper" 16
188	// bits (bit 16-31) of an external address, by encoding it into the instruction.
189	R_ADDRMIPSU
190	// R_ADDRMIPSTLS (only used on mips64) resolves to the low 16 bits of a TLS
191	// address (offset from thread pointer), by encoding it into the instruction.
192	R_ADDRMIPSTLS
193	// R_ADDRCUOFF resolves to a pointer-sized offset from the start of the
194	// symbol's DWARF compile unit.
195	R_ADDRCUOFF
196)
197
198// IsDirectJump returns whether r is a relocation for a direct jump.
199// A direct jump is a CALL or JMP instruction that takes the target address
200// as immediate. The address is embedded into the instruction, possibly
201// with limited width.
202// An indirect jump is a CALL or JMP instruction that takes the target address
203// in register or memory.
204func (r RelocType) IsDirectJump() bool {
205	switch r {
206	case R_CALL, R_CALLARM, R_CALLARM64, R_CALLPOWER, R_CALLMIPS, R_JMPMIPS:
207		return true
208	}
209	return false
210}
211