Skip to content

Commit 7bc06f8

Browse files
committed
[GR-34177] Don't differentiate between explicit and implicit relocation addend.
PullRequest: graal/9980
2 parents cd72f67 + c90fd63 commit 7bc06f8

23 files changed

+198
-334
lines changed

substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/BasicProgbitsSectionImpl.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,21 @@ public List<Section> getElements() {
113113
}
114114

115115
@Override
116-
public void markRelocationSite(int offset, RelocationKind k, String symbolName, boolean useImplicitAddend, Long explicitAddend) {
117-
((RelocatableSectionImpl) getElement()).markRelocationSite(offset, ByteBuffer.wrap(getContent()).order(getOwner().getByteOrder()), k, symbolName, useImplicitAddend,
118-
explicitAddend);
116+
public void markRelocationSite(int offset, RelocationKind k, String symbolName, long addend) {
117+
((RelocatableSectionImpl) getElement()).markRelocationSite(offset, ByteBuffer.wrap(getContent()).order(getOwner().getByteOrder()), k, symbolName,
118+
addend);
119119
}
120120

121121
@Override
122-
public final void markRelocationSite(int offset, ByteBuffer bb, RelocationKind k, String symbolName, boolean useImplicitAddend, Long explicitAddend) {
122+
public final void markRelocationSite(int offset, ByteBuffer bb, RelocationKind k, String symbolName, long addend) {
123123
assert getContent() == null || bb.array() == getContent();
124-
((RelocatableSectionImpl) getElement()).markRelocationSite(offset, bb, k, symbolName, useImplicitAddend, explicitAddend);
124+
((RelocatableSectionImpl) getElement()).markRelocationSite(offset, bb, k, symbolName, addend);
125125
}
126126

127127
@Override
128-
public Element getOrCreateRelocationElement(boolean useImplicitAddend) {
128+
public Element getOrCreateRelocationElement(long addend) {
129129
// FIXME: This looks suspicious: turning an Element back into an Impl?
130-
return ((RelocatableSectionImpl) getElement()).getOrCreateRelocationElement(useImplicitAddend);
130+
return ((RelocatableSectionImpl) getElement()).getOrCreateRelocationElement(addend);
131131
}
132132

133133
@Override

substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/ObjectFile.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,6 @@ public static int getRelocationSize(RelocationKind kind) {
358358
* Interface implemented by objects implementing a specific relocation method and size.
359359
*/
360360
public interface RelocationMethod {
361-
362-
boolean canUseImplicitAddend();
363-
364-
boolean canUseExplicitAddend();
365-
366361
/*
367362
* If we were implementing a linker, we'd have a method something like
368363
*
@@ -412,24 +407,22 @@ public interface RelocatableSectionImpl extends ElementImpl {
412407
* @param k the kind of fixup to be applied
413408
* @param symbolName the name of the symbol whose value is used to compute the fixed-up
414409
* bytes
415-
* @param useImplicitAddend whether the current bytes are to be used as an addend
416-
* @param explicitAddend a full-width addend, or null if useImplicitAddend is true
410+
* @param addend a full-width addend, or 0 if unneeded
417411
*/
418-
void markRelocationSite(int offset, ByteBuffer bb, RelocationKind k, String symbolName, boolean useImplicitAddend, Long explicitAddend);
412+
void markRelocationSite(int offset, ByteBuffer bb, RelocationKind k, String symbolName, long addend);
419413

420414
/**
421415
* Force the creation of a relocation section/element for this section, and return it. This
422416
* is necessary to avoid the on-demand instantiation of relocation sections at write()-time,
423417
* which leads to unpredictable results (e.g. not appearing in the ELF SHT, if the SHT was
424418
* written before the section was created).
425419
*
426-
* @param useImplicitAddend whether the relocation section of interest is for implicit
427-
* addends
420+
* @param addend whether a full-width addend, or 0 if unneeded
428421
*
429422
* @return the element which will hold relocation records (of the argument-specified kind)
430423
* for this section
431424
*/
432-
Element getOrCreateRelocationElement(boolean useImplicitAddend);
425+
Element getOrCreateRelocationElement(long addend);
433426
}
434427

435428
/**
@@ -446,7 +439,7 @@ public interface ProgbitsSectionImpl extends RelocatableSectionImpl {
446439
* passed a buffer. It uses the byte array accessed by {@link #getContent} and
447440
* {@link #setContent}.
448441
*/
449-
void markRelocationSite(int offset, RelocationKind k, String symbolName, boolean useImplicitAddend, Long explicitAddend);
442+
void markRelocationSite(int offset, RelocationKind k, String symbolName, long addend);
450443
}
451444

452445
public interface NobitsSectionImpl extends ElementImpl {

substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/StringSectionImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,17 @@ public StringSectionImpl() {
119119
*/
120120

121121
@Override
122-
public Element getOrCreateRelocationElement(boolean useImplicitAddend) {
122+
public Element getOrCreateRelocationElement(long addend) {
123123
throw new UnsupportedOperationException("can't mark relocaction sites in string section");
124124
}
125125

126126
@Override
127-
public void markRelocationSite(int offset, RelocationKind k, String symbolName, boolean useImplicitAddend, Long explicitAddend) {
127+
public void markRelocationSite(int offset, RelocationKind k, String symbolName, long addend) {
128128
throw new UnsupportedOperationException("can't mark relocaction sites in string section");
129129
}
130130

131131
@Override
132-
public void markRelocationSite(int offset, ByteBuffer bb, RelocationKind k, String symbolName, boolean useImplicitAddend, Long explicitAddend) {
132+
public void markRelocationSite(int offset, ByteBuffer bb, RelocationKind k, String symbolName, long addend) {
133133
throw new UnsupportedOperationException("can't mark relocaction sites in string section");
134134
}
135135

substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/elf/ELFMachine.java

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,6 @@
3333
* ELF machine type (incomplete). Each machine type also defines its set of relocation types.
3434
*/
3535
public enum ELFMachine/* implements Integral */ {
36-
NONE {
37-
@Override
38-
Class<? extends Enum<? extends RelocationMethod>> relocationTypes() {
39-
return ELFDummyRelocation.class;
40-
}
41-
},
4236
X86_64 {
4337
@Override
4438
Class<? extends Enum<? extends RelocationMethod>> relocationTypes() {
@@ -136,16 +130,13 @@ public static ELFRelocationMethod getRelocation(ELFMachine m, RelocationKind k)
136130

137131
}
138132
default:
139-
case NONE:
140-
return ELFDummyRelocation.R_NONE;
133+
throw new IllegalStateException("unknown ELF machine type");
141134
}
142135
}
143136

144137
// TODO: use explicit enum values
145138
public static ELFMachine from(int m) {
146139
switch (m) {
147-
case 0:
148-
return NONE;
149140
case 0x3E:
150141
return X86_64;
151142
case 0xB7:
@@ -156,9 +147,7 @@ public static ELFMachine from(int m) {
156147
}
157148

158149
public short toShort() {
159-
if (this == NONE) {
160-
return 0;
161-
} else if (this == AArch64) {
150+
if (this == AArch64) {
162151
return 0xB7;
163152
} else if (this == X86_64) {
164153
return 0x3E;
@@ -176,25 +165,6 @@ public static ELFMachine getSystemNativeValue() {
176165
}
177166
}
178167

179-
enum ELFDummyRelocation implements ELFRelocationMethod {
180-
R_NONE;
181-
182-
@Override
183-
public boolean canUseExplicitAddend() {
184-
return true;
185-
}
186-
187-
@Override
188-
public boolean canUseImplicitAddend() {
189-
return true;
190-
}
191-
192-
@Override
193-
public long toLong() {
194-
return ordinal();
195-
}
196-
}
197-
198168
enum ELFX86_64Relocation implements ELFRelocationMethod {
199169
// These are all named R_X86_64_... in elf.h,
200170
// but we just use R_... to keep it short.
@@ -245,19 +215,6 @@ enum ELFX86_64Relocation implements ELFRelocationMethod {
245215
assert R_COUNT.ordinal() == 39;
246216
}
247217

248-
/*
249-
* x86-64 relocs always use explicit addends.
250-
*/
251-
@Override
252-
public boolean canUseExplicitAddend() {
253-
return true;
254-
}
255-
256-
@Override
257-
public boolean canUseImplicitAddend() {
258-
return false;
259-
}
260-
261218
@Override
262219
public long toLong() {
263220
return ordinal();
@@ -398,16 +355,6 @@ enum ELFAArch64Relocation implements ELFRelocationMethod {
398355
this.code = code;
399356
}
400357

401-
@Override
402-
public boolean canUseImplicitAddend() {
403-
return false;
404-
}
405-
406-
@Override
407-
public boolean canUseExplicitAddend() {
408-
return true;
409-
}
410-
411358
@Override
412359
public long toLong() {
413360
return code;

substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/elf/ELFObjectFile.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
import java.util.Map;
3636
import java.util.Set;
3737

38+
import org.graalvm.nativeimage.ImageSingletons;
39+
import org.graalvm.nativeimage.Platform;
40+
3841
import com.oracle.objectfile.BuildDependency;
3942
import com.oracle.objectfile.ElementImpl;
4043
import com.oracle.objectfile.LayoutDecision;
@@ -45,15 +48,13 @@
4548
import com.oracle.objectfile.debuginfo.DebugInfoProvider;
4649
import com.oracle.objectfile.elf.dwarf.DwarfARangesSectionImpl;
4750
import com.oracle.objectfile.elf.dwarf.DwarfAbbrevSectionImpl;
51+
import com.oracle.objectfile.elf.dwarf.DwarfDebugInfo;
4852
import com.oracle.objectfile.elf.dwarf.DwarfFrameSectionImpl;
4953
import com.oracle.objectfile.elf.dwarf.DwarfInfoSectionImpl;
5054
import com.oracle.objectfile.elf.dwarf.DwarfLineSectionImpl;
51-
import com.oracle.objectfile.elf.dwarf.DwarfDebugInfo;
5255
import com.oracle.objectfile.elf.dwarf.DwarfStrSectionImpl;
5356
import com.oracle.objectfile.io.AssemblyBuffer;
5457
import com.oracle.objectfile.io.OutputAssembler;
55-
import org.graalvm.nativeimage.ImageSingletons;
56-
import org.graalvm.nativeimage.Platform;
5758

5859
/**
5960
* Represents an ELF object file (of any kind: relocatable, shared library, executable, core, ...).
@@ -469,9 +470,9 @@ public class ELFHeader extends ObjectFile.Header {
469470
*/
470471
class Struct {
471472

472-
IdentStruct ident = new IdentStruct();
473-
ELFType type;
474-
ELFMachine machine;
473+
final IdentStruct ident;
474+
final ELFType type;
475+
final ELFMachine machine;
475476
int version;
476477
long entry;
477478
long phoff;
@@ -484,10 +485,10 @@ class Struct {
484485
short shnum;
485486
short shstrndx;
486487

487-
Struct() {
488+
Struct(ELFType type, ELFMachine machine) {
488489
ident = new IdentStruct();
489-
type = ELFType.NONE;
490-
machine = ELFMachine.NONE;
490+
this.type = type;
491+
this.machine = machine;
491492
}
492493

493494
/**
@@ -618,16 +619,15 @@ public Iterable<BuildDependency> getDependencies(Map<Element, LayoutDecisionMap>
618619
public byte[] getOrDecideContent(Map<Element, LayoutDecisionMap> alreadyDecided, byte[] contentHint) {
619620
// we serialize ourselves by writing a Struct to a bytebuffer
620621
OutputAssembler oa = AssemblyBuffer.createOutputAssembler(getDataEncoding().toByteOrder());
621-
Struct contents = new Struct(); // also creates ident struct, which we need to populate
622+
/* Also creates ident struct, which we need to populate. */
623+
Struct contents = new Struct(getType(), getMachine());
622624

623625
// don't assign magic -- its default value is correct
624626
contents.ident.fileClass = getFileClass();
625627
contents.ident.dataEncoding = getDataEncoding();
626628
contents.ident.version = getVersion();
627629
contents.ident.osabi = getOsAbi();
628630
contents.ident.abiVersion = (char) getAbiVersion();
629-
contents.type = getType();
630-
contents.machine = getMachine();
631631
contents.version = getVersion();
632632
contents.entry = 0;
633633
contents.shoff = (int) alreadyDecided.get(sht).getDecidedValue(LayoutDecision.Kind.OFFSET);
@@ -668,7 +668,7 @@ public int getOrDecideOffset(Map<Element, LayoutDecisionMap> alreadyDecided, int
668668

669669
@Override
670670
public int getOrDecideSize(Map<Element, LayoutDecisionMap> alreadyDecided, int sizeHint) {
671-
int size = (new Struct()).getWrittenSize();
671+
int size = (new Struct(getType(), getMachine())).getWrittenSize();
672672
assert sizeHint == -1 || sizeHint == size;
673673
return size;
674674
}
@@ -1193,13 +1193,19 @@ public void installDebugInfo(DebugInfoProvider debugInfoProvider) {
11931193
* decision set and causes an NPE during reloc section write. So we need to create the
11941194
* relevant reloc sections here in advance.
11951195
*/
1196-
elfStrSectionImpl.getOrCreateRelocationElement(false);
1197-
elfAbbrevSectionImpl.getOrCreateRelocationElement(false);
1198-
frameSectionImpl.getOrCreateRelocationElement(false);
1199-
elfInfoSectionImpl.getOrCreateRelocationElement(false);
1200-
elfARangesSectionImpl.getOrCreateRelocationElement(false);
1201-
elfLineSectionImpl.getOrCreateRelocationElement(false);
1196+
elfStrSectionImpl.getOrCreateRelocationElement(0);
1197+
elfAbbrevSectionImpl.getOrCreateRelocationElement(0);
1198+
frameSectionImpl.getOrCreateRelocationElement(0);
1199+
elfInfoSectionImpl.getOrCreateRelocationElement(0);
1200+
elfARangesSectionImpl.getOrCreateRelocationElement(0);
1201+
elfLineSectionImpl.getOrCreateRelocationElement(0);
12021202
/* Ok now we can populate the debug info model. */
12031203
dwarfSections.installDebugInfo(debugInfoProvider);
12041204
}
1205+
1206+
@SuppressWarnings("unused")
1207+
static boolean useExplicitAddend(long addend) {
1208+
// For now, we are always using explicit addends
1209+
return true;
1210+
}
12051211
}

substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/elf/ELFProgbitsSection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void setContent(byte[] c) {
6969
}
7070

7171
@Override
72-
public void markRelocationSite(int offset, ObjectFile.RelocationKind k, String symbolName, boolean useImplicitAddend, Long explicitAddend) {
73-
markRelocationSite(offset, ByteBuffer.wrap(getContent()).order(getOwner().getByteOrder()), k, symbolName, useImplicitAddend, explicitAddend);
72+
public void markRelocationSite(int offset, ObjectFile.RelocationKind k, String symbolName, long addend) {
73+
markRelocationSite(offset, ByteBuffer.wrap(getContent()).order(getOwner().getByteOrder()), k, symbolName, addend);
7474
}
7575
}

substratevm/src/com.oracle.objectfile/src/com/oracle/objectfile/elf/ELFRelocationSection.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ int getWrittenSize() {
108108

109109
interface ELFRelocationMethod extends RelocationMethod {
110110

111+
/*
112+
* For now, ELF relocations are always using explicit addends. This can change in the
113+
* future.
114+
*/
115+
116+
default boolean canUseImplicitAddend() {
117+
return false;
118+
}
119+
120+
default boolean canUseExplicitAddend() {
121+
return true;
122+
}
123+
111124
long toLong();
112125
}
113126

@@ -172,24 +185,24 @@ public int hashCode() {
172185
}
173186
}
174187

175-
public Entry addEntry(ELFSection s, long offset, ELFRelocationMethod t, ELFSymtab.Entry sym, Long explicitAddend) {
176-
if (explicitAddend != null) {
188+
void addEntry(ELFSection s, long offset, ELFRelocationMethod t, ELFSymtab.Entry sym, long addend) {
189+
if (ELFObjectFile.useExplicitAddend(addend)) {
177190
if (!t.canUseExplicitAddend()) {
178191
throw new IllegalArgumentException("cannot use relocation method " + t + " with explicit addends");
179192
}
180193
if (!withExplicitAddends) {
181194
throw new IllegalStateException("cannot create relocation with addend in .rel section");
182195
}
183196
} else {
197+
// use implicit addend
184198
if (!t.canUseImplicitAddend()) {
185199
throw new IllegalArgumentException("cannot use relocation method " + t + " with implicit addends");
186200
}
187201
if (withExplicitAddends) {
188202
throw new IllegalStateException("cannot create relocation without addend in .rela section");
189203
}
190204
}
191-
long addend = (explicitAddend != null) ? explicitAddend : 0L;
192-
return entries.computeIfAbsent(new Entry(s, offset, t, sym, addend), Function.identity());
205+
entries.computeIfAbsent(new Entry(s, offset, t, sym, addend), Function.identity());
193206
}
194207

195208
public boolean isDynamic() {

0 commit comments

Comments
 (0)