Skip to content

Commit 3b37ff4

Browse files
erifan01ianlancetaylor
erifan01
authored andcommitted
cmd/link: increase the reserved space for ELF relocations
Currently the offset values of ELF relocations and Macho relocations are 256 and 512 respectively, which means that the space reserved for ELF relocations is only 256. But AARCH64 has more than 256 ELF relocation types, in fact the maximum AARCH64 ELF relocation type recorded in file src/debug/elf/elf.go is 1032 currently. So this CL increases the offset of Macho relocations to 2048 to leave enough space for AARCH64 ELF relocations. Change-Id: I784ac38aeb3e102ac7825f6d621086849c8d3146 Reviewed-on: https://go-review.googlesource.com/c/go/+/172497 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
1 parent b5946ed commit 3b37ff4

File tree

11 files changed

+109
-101
lines changed

11 files changed

+109
-101
lines changed

src/cmd/internal/objabi/util.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ var (
3434
Version = version
3535
)
3636

37+
const (
38+
ElfRelocOffset = 256
39+
MachoRelocOffset = 2048 // reserve enough space for ELF relocations
40+
)
41+
3742
func goarm() int {
3843
switch v := envOr("GOARM", defaultGOARM); v {
3944
case "5":

src/cmd/link/internal/amd64/asm.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
103103

104104
switch r.Type {
105105
default:
106-
if r.Type >= 256 {
106+
if r.Type >= objabi.ElfRelocOffset {
107107
ld.Errorf(s, "unexpected relocation type %d (%s)", r.Type, sym.RelocName(ctxt.Arch, r.Type))
108108
return false
109109
}
110110

111111
// Handle relocations found in ELF object files.
112-
case 256 + objabi.RelocType(elf.R_X86_64_PC32):
112+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_X86_64_PC32):
113113
if targ.Type == sym.SDYNIMPORT {
114114
ld.Errorf(s, "unexpected R_X86_64_PC32 relocation for dynamic symbol %s", targ.Name)
115115
}
@@ -122,7 +122,7 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
122122
r.Add += 4
123123
return true
124124

125-
case 256 + objabi.RelocType(elf.R_X86_64_PC64):
125+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_X86_64_PC64):
126126
if targ.Type == sym.SDYNIMPORT {
127127
ld.Errorf(s, "unexpected R_X86_64_PC64 relocation for dynamic symbol %s", targ.Name)
128128
}
@@ -133,7 +133,7 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
133133
r.Add += 8
134134
return true
135135

136-
case 256 + objabi.RelocType(elf.R_X86_64_PLT32):
136+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_X86_64_PLT32):
137137
r.Type = objabi.R_PCREL
138138
r.Add += 4
139139
if targ.Type == sym.SDYNIMPORT {
@@ -144,7 +144,9 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
144144

145145
return true
146146

147-
case 256 + objabi.RelocType(elf.R_X86_64_GOTPCREL), 256 + objabi.RelocType(elf.R_X86_64_GOTPCRELX), 256 + objabi.RelocType(elf.R_X86_64_REX_GOTPCRELX):
147+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_X86_64_GOTPCREL),
148+
objabi.ElfRelocOffset + objabi.RelocType(elf.R_X86_64_GOTPCRELX),
149+
objabi.ElfRelocOffset + objabi.RelocType(elf.R_X86_64_REX_GOTPCRELX):
148150
if targ.Type != sym.SDYNIMPORT {
149151
// have symbol
150152
if r.Off >= 2 && s.P[r.Off-2] == 0x8b {
@@ -167,17 +169,17 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
167169
r.Add += int64(targ.Got())
168170
return true
169171

170-
case 256 + objabi.RelocType(elf.R_X86_64_64):
172+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_X86_64_64):
171173
if targ.Type == sym.SDYNIMPORT {
172174
ld.Errorf(s, "unexpected R_X86_64_64 relocation for dynamic symbol %s", targ.Name)
173175
}
174176
r.Type = objabi.R_ADDR
175177
return true
176178

177179
// Handle relocations found in Mach-O object files.
178-
case 512 + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 0,
179-
512 + ld.MACHO_X86_64_RELOC_SIGNED*2 + 0,
180-
512 + ld.MACHO_X86_64_RELOC_BRANCH*2 + 0:
180+
case objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 0,
181+
objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_SIGNED*2 + 0,
182+
objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_BRANCH*2 + 0:
181183
// TODO: What is the difference between all these?
182184
r.Type = objabi.R_ADDR
183185

@@ -186,7 +188,7 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
186188
}
187189
return true
188190

189-
case 512 + ld.MACHO_X86_64_RELOC_BRANCH*2 + 1:
191+
case objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_BRANCH*2 + 1:
190192
if targ.Type == sym.SDYNIMPORT {
191193
addpltsym(ctxt, targ)
192194
r.Sym = ctxt.Syms.Lookup(".plt", 0)
@@ -196,19 +198,19 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
196198
}
197199
fallthrough
198200

199-
case 512 + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 1,
200-
512 + ld.MACHO_X86_64_RELOC_SIGNED*2 + 1,
201-
512 + ld.MACHO_X86_64_RELOC_SIGNED_1*2 + 1,
202-
512 + ld.MACHO_X86_64_RELOC_SIGNED_2*2 + 1,
203-
512 + ld.MACHO_X86_64_RELOC_SIGNED_4*2 + 1:
201+
case objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 1,
202+
objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_SIGNED*2 + 1,
203+
objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_SIGNED_1*2 + 1,
204+
objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_SIGNED_2*2 + 1,
205+
objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_SIGNED_4*2 + 1:
204206
r.Type = objabi.R_PCREL
205207

206208
if targ.Type == sym.SDYNIMPORT {
207209
ld.Errorf(s, "unexpected pc-relative reloc for dynamic symbol %s", targ.Name)
208210
}
209211
return true
210212

211-
case 512 + ld.MACHO_X86_64_RELOC_GOT_LOAD*2 + 1:
213+
case objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_GOT_LOAD*2 + 1:
212214
if targ.Type != sym.SDYNIMPORT {
213215
// have symbol
214216
// turn MOVQ of GOT entry into LEAQ of symbol itself
@@ -223,7 +225,7 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
223225
}
224226
fallthrough
225227

226-
case 512 + ld.MACHO_X86_64_RELOC_GOT*2 + 1:
228+
case objabi.MachoRelocOffset + ld.MACHO_X86_64_RELOC_GOT*2 + 1:
227229
if targ.Type != sym.SDYNIMPORT {
228230
ld.Errorf(s, "unexpected GOT reloc for non-dynamic symbol %s", targ.Name)
229231
}
@@ -333,7 +335,7 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
333335
rela.AddUint64(ctxt.Arch, ld.ELF64_R_INFO(uint32(targ.Dynid), uint32(elf.R_X86_64_32)))
334336
}
335337
rela.AddUint64(ctxt.Arch, uint64(r.Add))
336-
r.Type = 256 // ignore during relocsym
338+
r.Type = objabi.ElfRelocOffset // ignore during relocsym
337339
return true
338340
}
339341

@@ -359,7 +361,7 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
359361
s.Value = got.Size
360362
got.AddUint64(ctxt.Arch, 0)
361363
ctxt.Syms.Lookup(".linkedit.got", 0).AddUint32(ctxt.Arch, uint32(targ.Dynid))
362-
r.Type = 256 // ignore during relocsym
364+
r.Type = objabi.ElfRelocOffset // ignore during relocsym
363365
return true
364366
}
365367
}

src/cmd/link/internal/arm/asm.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
120120

121121
switch r.Type {
122122
default:
123-
if r.Type >= 256 {
123+
if r.Type >= objabi.ElfRelocOffset {
124124
ld.Errorf(s, "unexpected relocation type %d (%s)", r.Type, sym.RelocName(ctxt.Arch, r.Type))
125125
return false
126126
}
127127

128128
// Handle relocations found in ELF object files.
129-
case 256 + objabi.RelocType(elf.R_ARM_PLT32):
129+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_PLT32):
130130
r.Type = objabi.R_CALLARM
131131

132132
if targ.Type == sym.SDYNIMPORT {
@@ -137,11 +137,11 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
137137

138138
return true
139139

140-
case 256 + objabi.RelocType(elf.R_ARM_THM_PC22): // R_ARM_THM_CALL
140+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_THM_PC22): // R_ARM_THM_CALL
141141
ld.Exitf("R_ARM_THM_CALL, are you using -marm?")
142142
return false
143143

144-
case 256 + objabi.RelocType(elf.R_ARM_GOT32): // R_ARM_GOT_BREL
144+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_GOT32): // R_ARM_GOT_BREL
145145
if targ.Type != sym.SDYNIMPORT {
146146
addgotsyminternal(ctxt, targ)
147147
} else {
@@ -153,7 +153,7 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
153153
r.Add += int64(targ.Got())
154154
return true
155155

156-
case 256 + objabi.RelocType(elf.R_ARM_GOT_PREL): // GOT(nil) + A - nil
156+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_GOT_PREL): // GOT(nil) + A - nil
157157
if targ.Type != sym.SDYNIMPORT {
158158
addgotsyminternal(ctxt, targ)
159159
} else {
@@ -165,19 +165,19 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
165165
r.Add += int64(targ.Got()) + 4
166166
return true
167167

168-
case 256 + objabi.RelocType(elf.R_ARM_GOTOFF): // R_ARM_GOTOFF32
168+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_GOTOFF): // R_ARM_GOTOFF32
169169
r.Type = objabi.R_GOTOFF
170170

171171
return true
172172

173-
case 256 + objabi.RelocType(elf.R_ARM_GOTPC): // R_ARM_BASE_PREL
173+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_GOTPC): // R_ARM_BASE_PREL
174174
r.Type = objabi.R_PCREL
175175

176176
r.Sym = ctxt.Syms.Lookup(".got", 0)
177177
r.Add += 4
178178
return true
179179

180-
case 256 + objabi.RelocType(elf.R_ARM_CALL):
180+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_CALL):
181181
r.Type = objabi.R_CALLARM
182182
if targ.Type == sym.SDYNIMPORT {
183183
addpltsym(ctxt, targ)
@@ -187,21 +187,21 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
187187

188188
return true
189189

190-
case 256 + objabi.RelocType(elf.R_ARM_REL32): // R_ARM_REL32
190+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_REL32): // R_ARM_REL32
191191
r.Type = objabi.R_PCREL
192192

193193
r.Add += 4
194194
return true
195195

196-
case 256 + objabi.RelocType(elf.R_ARM_ABS32):
196+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_ABS32):
197197
if targ.Type == sym.SDYNIMPORT {
198198
ld.Errorf(s, "unexpected R_ARM_ABS32 relocation for dynamic symbol %s", targ.Name)
199199
}
200200
r.Type = objabi.R_ADDR
201201
return true
202202

203203
// we can just ignore this, because we are targeting ARM V5+ anyway
204-
case 256 + objabi.RelocType(elf.R_ARM_V4BX):
204+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_V4BX):
205205
if r.Sym != nil {
206206
// R_ARM_V4BX is ABS relocation, so this symbol is a dummy symbol, ignore it
207207
r.Sym.Type = 0
@@ -210,8 +210,8 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
210210
r.Sym = nil
211211
return true
212212

213-
case 256 + objabi.RelocType(elf.R_ARM_PC24),
214-
256 + objabi.RelocType(elf.R_ARM_JUMP24):
213+
case objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_PC24),
214+
objabi.ElfRelocOffset + objabi.RelocType(elf.R_ARM_JUMP24):
215215
r.Type = objabi.R_CALLARM
216216
if targ.Type == sym.SDYNIMPORT {
217217
addpltsym(ctxt, targ)

src/cmd/link/internal/ld/data.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func relocsym(ctxt *Link, s *sym.Symbol) {
162162
}
163163
}
164164

165-
if r.Type >= 256 {
165+
if r.Type >= objabi.ElfRelocOffset {
166166
continue
167167
}
168168
if r.Siz == 0 { // informational relocation - no work to do
@@ -636,7 +636,7 @@ func dynrelocsym(ctxt *Link, s *sym.Symbol) {
636636
continue
637637
}
638638

639-
if r.Sym != nil && r.Sym.Type == sym.SDYNIMPORT || r.Type >= 256 {
639+
if r.Sym != nil && r.Sym.Type == sym.SDYNIMPORT || r.Type >= objabi.ElfRelocOffset {
640640
if r.Sym != nil && !r.Sym.Attr.Reachable() {
641641
Errorf(s, "dynamic relocation to unreachable symbol %s", r.Sym.Name)
642642
}

src/cmd/link/internal/ld/pe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,7 @@ func addPEBaseRelocSym(ctxt *Link, s *sym.Symbol, rt *peBaseRelocTable) {
14011401
if !r.Sym.Attr.Reachable() {
14021402
continue
14031403
}
1404-
if r.Type >= 256 {
1404+
if r.Type >= objabi.ElfRelocOffset {
14051405
continue
14061406
}
14071407
if r.Siz == 0 { // informational relocation

src/cmd/link/internal/loadelf/ldelf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length i
923923
rp.Sym = elfsym.sym
924924
}
925925

926-
rp.Type = 256 + objabi.RelocType(info)
926+
rp.Type = objabi.ElfRelocOffset + objabi.RelocType(info)
927927
rp.Siz, err = relSize(arch, pn, uint32(info))
928928
if err != nil {
929929
return nil, 0, err

src/cmd/link/internal/loadmacho/ldmacho.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length i
771771
// handle reference to __IMPORT/__pointers.
772772
// how much worse can this get?
773773
// why are we supporting 386 on the mac anyway?
774-
rp.Type = 512 + MACHO_FAKE_GOTPCREL
774+
rp.Type = objabi.MachoRelocOffset + MACHO_FAKE_GOTPCREL
775775

776776
// figure out which pointer this is a reference to.
777777
k = int(uint64(ks.res1) + (uint64(rel.value)-ks.addr)/4)
@@ -805,7 +805,7 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length i
805805
}
806806

807807
rp.Siz = rel.length
808-
rp.Type = 512 + (objabi.RelocType(rel.type_) << 1) + objabi.RelocType(rel.pcrel)
808+
rp.Type = objabi.MachoRelocOffset + (objabi.RelocType(rel.type_) << 1) + objabi.RelocType(rel.pcrel)
809809
rp.Off = int32(rel.addr)
810810

811811
// Handle X86_64_RELOC_SIGNED referencing a section (rel->extrn == 0).

0 commit comments

Comments
 (0)