@@ -178,21 +178,20 @@ final class RelocationInfo implements RelocationRecord, RelocationMethod {
178178 private final Symbol sym ;
179179 private final MachOSection targetSection ;
180180 private final byte log2length ;
181+ private final Long addend ; // `null` for kinds other than ADDEND
181182
182- public Long getAddend () {
183- return addend ;
184- }
185-
186- private final Long addend ;
187-
183+ /**
184+ * Construct a relocation record for ARM64_RELOC_ADDEND.
185+ *
186+ * @param containingElement the containing relocation element
187+ * @param relocatedSection the section being relocated
188+ * @param offset the offset, within the relocated section, of the relocation site
189+ * @param addend the addend to be encoded
190+ */
188191 public static RelocationInfo newAddend (MachORelocationElement containingElement , MachOSection relocatedSection , int offset , int requestedLength , long addend ) {
189192 return new RelocationInfo (containingElement , relocatedSection , offset , requestedLength , addend );
190193 }
191-
192- private RelocationInfo (MachORelocationElement containingElement , MachOSection relocatedSection , int offset , int requestedLength , long addend ) {
193- this .containingElement = containingElement ;
194- this .relocatedSection = relocatedSection ;
195- this .sectionOffset = offset ; // gets turned into a vaddr on write-out
194+ private static byte encodeRequestedLength (int requestedLength ) {
196195 /*
197196 * NOTE: the Mach-O spec claims that r_length == 3 means a 4-byte length and not an 8-byte
198197 * length. But it doesn't say how to encode an 8-bytes-long relocation site. And the
@@ -206,8 +205,23 @@ private RelocationInfo(MachORelocationElement containingElement, MachOSection re
206205 if (requestedLength != 8 && requestedLength != 4 && requestedLength != 2 && requestedLength != 1 ) {
207206 throw new IllegalArgumentException ("Mach-O cannot represent relocation lengths other than {1,2,4,8} bytes" );
208207 }
209- this .log2length = (byte ) ((requestedLength == 8 ) ? 3 : (requestedLength == 4 ) ? 2 : (requestedLength == 2 ) ? 1 : 0 );
210- this .kind = RelocationKind .UNKNOWN ;
208+ return (byte ) ((requestedLength == 8 ) ? 3 : (requestedLength == 4 ) ? 2 : (requestedLength == 2 ) ? 1 : 0 );
209+ }
210+
211+ /**
212+ * Construct a relocation record for ARM64_RELOC_ADDEND.
213+ *
214+ * @param containingElement the containing relocation element
215+ * @param relocatedSection the section being relocated
216+ * @param offset the offset, within the relocated section, of the relocation site
217+ * @param addend the addend to be encoded
218+ */
219+ private RelocationInfo (MachORelocationElement containingElement , MachOSection relocatedSection , int offset , int requestedLength , long addend ) {
220+ this .containingElement = containingElement ;
221+ this .relocatedSection = relocatedSection ;
222+ this .sectionOffset = offset ; // gets turned into a vaddr on write-out
223+ this .log2length = encodeRequestedLength (requestedLength );
224+ this .kind = RelocationKind .UNKNOWN ; // Placeholder
211225 this .targetSection = null ;
212226 this .sym = null ;
213227 this .addend = addend ;
@@ -227,20 +241,7 @@ private RelocationInfo(MachORelocationElement containingElement, MachOSection re
227241 this .containingElement = containingElement ;
228242 this .relocatedSection = relocatedSection ;
229243 this .sectionOffset = offset ; // gets turned into a vaddr on write-out
230- /*
231- * NOTE: the Mach-O spec claims that r_length == 3 means a 4-byte length and not an 8-byte
232- * length. But it doesn't say how to encode an 8-bytes-long relocation site. And the
233- * following link seems to suggest that in the case of x86-64, r_length==3 does encode the
234- * 8-byte case.
235- * http://www.opensource.apple.com/source/xnu/xnu-1456.1.26/EXTERNAL_HEADERS/mach
236- * -o/x86_64/reloc.h
237- *
238- * Experimenting....
239- */
240- if (requestedLength != 8 && requestedLength != 4 && requestedLength != 2 && requestedLength != 1 ) {
241- throw new IllegalArgumentException ("Mach-O cannot represent relocation lengths other than {1,2,4,8} bytes" );
242- }
243- this .log2length = (byte ) ((requestedLength == 8 ) ? 3 : (requestedLength == 4 ) ? 2 : (requestedLength == 2 ) ? 1 : 0 );
244+ this .log2length = encodeRequestedLength (requestedLength );;
244245 this .kind = kind ;
245246 SymbolTable symtab = relocatedSection .getOwner ().getSymbolTable ();
246247 // FIXME: also allow section numbers here, for non-extern symbols
@@ -326,6 +327,10 @@ public MachOSection getRelocatedSection() {
326327 return relocatedSection ;
327328 }
328329
330+ public Long getAddend () {
331+ return addend ;
332+ }
333+
329334 private boolean isExtern () {
330335 // we record localness by grabbing the target section (see constructor)
331336 return targetSection == null && addend == null ;
0 commit comments