1
1
/*
2
- * Copyright (c) 2001, 2004 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2001, 2020 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -116,13 +116,13 @@ class ELFHeaderImpl implements ELFHeader {
116
116
private int version ; // Elf32_Word
117
117
/** Virtual address to which the system first transfers control.
118
118
* If there is no entry point for the file the value is 0. */
119
- private int entry_point ; // Elf32_Addr
119
+ private long entry_point ; // Elf32_Addr
120
120
/** Program header table offset in bytes. If there is no program
121
121
* header table the value is 0. */
122
- private int ph_offset ; // Elf32_Off
122
+ private long ph_offset ; // Elf32_Off
123
123
/** Section header table offset in bytes. If there is no section
124
124
* header table the value is 0. */
125
- private int sh_offset ; // Elf32_Off
125
+ private long sh_offset ; // Elf32_Off
126
126
/** Processor specific flags. */
127
127
private int flags ; // Elf32_Word
128
128
/** ELF header size in bytes. */
@@ -165,9 +165,9 @@ class ELFHeaderImpl implements ELFHeader {
165
165
file_type = readShort ();
166
166
arch = readShort ();
167
167
version = readInt ();
168
- entry_point = readInt ();
169
- ph_offset = readInt ();
170
- sh_offset = readInt ();
168
+ entry_point = readWord ();
169
+ ph_offset = readWord ();
170
+ sh_offset = readWord ();
171
171
flags = readInt ();
172
172
eh_size = readShort ();
173
173
ph_entry_size = readShort ();
@@ -384,23 +384,23 @@ class ELFSectionHeaderImpl implements ELFSectionHeader {
384
384
/** Section content and semantics. */
385
385
private int type ; // Elf32_Word
386
386
/** Flags. */
387
- private int flags ; // Elf32_Word
387
+ private long flags ; // Elf32_Word
388
388
/** If the section will be in the memory image of a process this
389
389
* will be the address at which the first byte of section will be
390
390
* loaded. Otherwise, this value is 0. */
391
- private int address ; // Elf32_Addr
391
+ private long address ; // Elf32_Addr
392
392
/** Offset from beginning of file to first byte of the section. */
393
- private int section_offset ; // Elf32_Off
393
+ private long section_offset ; // Elf32_Off
394
394
/** Size in bytes of the section. TYPE_NOBITS is a special case. */
395
- private int size ; // Elf32_Word
395
+ private long size ; // Elf32_Word
396
396
/** Section header table index link. */
397
397
private int link ; // Elf32_Word
398
398
/** Extra information determined by the section type. */
399
399
private int info ; // Elf32_Word
400
400
/** Address alignment constraints for the section. */
401
- private int address_alignment ; // Elf32_Word
401
+ private long address_alignment ; // Elf32_Word
402
402
/** Size of a fixed-size entry, 0 if none. */
403
- private int entry_size ; // Elf32_Word
403
+ private long entry_size ; // Elf32_Word
404
404
405
405
/** Memoized symbol table. */
406
406
private MemoizedObject [] symbols ;
@@ -416,14 +416,14 @@ class ELFSectionHeaderImpl implements ELFSectionHeader {
416
416
seek (offset );
417
417
name_ndx = readInt ();
418
418
type = readInt ();
419
- flags = readInt ();
420
- address = readInt ();
421
- section_offset = readInt ();
422
- size = readInt ();
419
+ flags = readWord ();
420
+ address = readWord ();
421
+ section_offset = readWord ();
422
+ size = readWord ();
423
423
link = readInt ();
424
424
info = readInt ();
425
- address_alignment = readInt ();
426
- entry_size = readInt ();
425
+ address_alignment = readWord ();
426
+ entry_size = readWord ();
427
427
428
428
switch (type ) {
429
429
case ELFSectionHeader .TYPE_NULL :
@@ -433,10 +433,10 @@ class ELFSectionHeaderImpl implements ELFSectionHeader {
433
433
case ELFSectionHeader .TYPE_SYMTBL :
434
434
case ELFSectionHeader .TYPE_DYNSYM :
435
435
// Setup the symbol table.
436
- int num_entries = size / entry_size ;
436
+ int num_entries = ( int )( size / entry_size ) ;
437
437
symbols = new MemoizedObject [num_entries ];
438
438
for (int i = 0 ; i < num_entries ; i ++) {
439
- final int symbolOffset = section_offset +
439
+ final long symbolOffset = section_offset +
440
440
(i * entry_size );
441
441
symbols [i ] = new MemoizedObject () {
442
442
public Object computeValue () {
@@ -447,24 +447,26 @@ public Object computeValue() {
447
447
break ;
448
448
case ELFSectionHeader .TYPE_STRTBL :
449
449
// Setup the string table.
450
- final int strTableOffset = section_offset ;
451
- final int strTableSize = size ;
450
+ final long strTableOffset = section_offset ;
451
+ final long strTableSize = size ;
452
+ assert32bitLong (strTableSize ); // must fit in 32-bits
452
453
stringTable = new MemoizedObject () {
453
454
public Object computeValue () {
454
455
return new ELFStringTableImpl (strTableOffset ,
455
- strTableSize );
456
+ ( int ) strTableSize );
456
457
}
457
458
};
458
459
break ;
459
460
case ELFSectionHeader .TYPE_RELO_EXPLICIT :
460
461
break ;
461
462
case ELFSectionHeader .TYPE_HASH :
462
- final int hashTableOffset = section_offset ;
463
- final int hashTableSize = size ;
463
+ final long hashTableOffset = section_offset ;
464
+ final long hashTableSize = size ;
465
+ assert32bitLong (hashTableSize ); // must fit in 32-bits
464
466
hashTable = new MemoizedObject () {
465
467
public Object computeValue () {
466
468
return new ELFHashTableImpl (hashTableOffset ,
467
- hashTableSize );
469
+ ( int ) hashTableSize );
468
470
}
469
471
};
470
472
break ;
@@ -531,7 +533,7 @@ public int getLink() {
531
533
return link ;
532
534
}
533
535
534
- public int getOffset () {
536
+ public long getOffset () {
535
537
return section_offset ;
536
538
}
537
539
}
@@ -625,10 +627,10 @@ class ELFSymbolImpl implements ELFSymbol {
625
627
private int name_ndx ; // Elf32_Word
626
628
/** Value of the associated symbol. This may be an address or
627
629
* an absolute value. */
628
- private int value ; // Elf32_Addr
630
+ private long value ; // Elf32_Addr
629
631
/** Size of the symbol. 0 if the symbol has no size or the size
630
632
* is unknown. */
631
- private int size ; // Elf32_Word
633
+ private long size ; // Elf32_Word
632
634
/** Specifies the symbol type and beinding attributes. */
633
635
private byte info ; // unsigned char
634
636
/** Currently holds the value of 0 and has no meaning. */
@@ -646,12 +648,28 @@ class ELFSymbolImpl implements ELFSymbol {
646
648
ELFSymbolImpl (long offset , int section_type ) throws ELFException {
647
649
seek (offset );
648
650
this .offset = offset ;
649
- name_ndx = readInt ();
650
- value = readInt ();
651
- size = readInt ();
652
- info = readByte ();
653
- other = readByte ();
654
- section_header_ndx = readShort ();
651
+ switch (getObjectSize ()) {
652
+ case CLASS_32 : {
653
+ name_ndx = readInt ();
654
+ value = readInt ();
655
+ size = readInt ();
656
+ info = readByte ();
657
+ other = readByte ();
658
+ section_header_ndx = readShort ();
659
+ break ;
660
+ }
661
+ case CLASS_64 : {
662
+ name_ndx = readInt ();
663
+ info = readByte ();
664
+ other = readByte ();
665
+ section_header_ndx = readShort ();
666
+ value = readWord ();
667
+ size = readWord ();
668
+ break ;
669
+ }
670
+ default :
671
+ throw new ELFException ("Invalid Object Size." );
672
+ }
655
673
656
674
this .section_type = section_type ;
657
675
@@ -701,7 +719,7 @@ public long getValue() {
701
719
return value ;
702
720
}
703
721
704
- public int getSize () {
722
+ public long getSize () {
705
723
return size ;
706
724
}
707
725
}
@@ -923,6 +941,17 @@ long readLong() throws ELFException {
923
941
}
924
942
}
925
943
944
+ long readWord () throws ELFException {
945
+ switch (getObjectSize ()) {
946
+ case CLASS_32 :
947
+ return readInt ();
948
+ case CLASS_64 :
949
+ return readLong ();
950
+ default :
951
+ throw new ELFException ("Invalid Object Size." );
952
+ }
953
+ }
954
+
926
955
/** Signed byte utility functions used for converting from big-endian
927
956
* (MSB) to little-endian (LSB). */
928
957
short byteSwap (short arg ) {
@@ -1030,6 +1059,12 @@ long unsignedByteSwap(int arg) {
1030
1059
return (long )(((long )unsignedByteSwap ((short )arg )) << 16 ) |
1031
1060
((long )unsignedByteSwap ((short )(arg >>> 16 )));
1032
1061
}
1062
+
1063
+ void assert32bitLong (long x ) {
1064
+ if (x != (long )(int )x ) {
1065
+ throw new ELFException ("64-bit value does not fit in 32-bits: " + x );
1066
+ }
1067
+ }
1033
1068
}
1034
1069
1035
1070
public static void main (String args []) {
0 commit comments