11# frozen_string_literal: true
22
33require 'bindata'
4+
45module ELFTools
56 # Define ELF related structures in this module.
67 #
78 # Structures are fetched from https://github.com/torvalds/linux/blob/master/include/uapi/linux/elf.h.
8- # Using the bindata gem to make these structures support 32/64 bits and
9- # little/big endian simultaneously.
9+ # Use gem +bindata+ to have these structures support 32/64 bits and little/big endian simultaneously.
1010 module Structs
1111 # The base structure to define common methods.
1212 class ELFStruct < BinData ::Record
@@ -25,9 +25,13 @@ def patches
2525 end
2626
2727 class << self
28- # Hook constructor, while +BinData::Record+ doesn't allow us to override +#initialize+,
29- # so we hack +new+ here.
30- def new ( **kwargs )
28+ # Hooks the constructor.
29+ #
30+ # +BinData::Record+ doesn't allow us to override +#initialize+, so we hack +new+ here.
31+ def new ( *args )
32+ # XXX: The better implementation is +new(*args, **kwargs)+, but we can't do this unless bindata changed
33+ # lib/bindata/dsl.rb#override_new_in_class to invoke +new+ with both +args+ and +kwargs+.
34+ kwargs = args . last . is_a? ( Hash ) ? args . last : { }
3135 offset = kwargs . delete ( :offset )
3236 super . tap do |obj |
3337 obj . offset = offset
@@ -44,13 +48,13 @@ def new(**kwargs)
4448 end
4549 end
4650
47- # Hacking to get endian of current class
48- # @return [Symbol, nil] + :little+ or + :big+ .
51+ # Gets the endianness of current class.
52+ # @return [:little, :big] The endianness .
4953 def self_endian
5054 bindata_name [ -2 ..-1 ] == 'be' ? :big : :little
5155 end
5256
53- # Pack integer into string.
57+ # Packs an integer to string.
5458 # @param [Integer] val
5559 # @param [Integer] bytes
5660 # @return [String]
@@ -112,7 +116,7 @@ class ELF_Shdr < ELFStruct
112116 choice :sh_entsize , **CHOICE_SIZE_T
113117 end
114118
115- # Program header structure for 32bit .
119+ # Program header structure for 32-bit .
116120 class ELF32_Phdr < ELFStruct
117121 endian :big_and_little
118122 uint32 :p_type
@@ -125,7 +129,7 @@ class ELF32_Phdr < ELFStruct
125129 uint32 :p_align
126130 end
127131
128- # Program header structure for 64bit .
132+ # Program header structure for 64-bit .
129133 class ELF64_Phdr < ELFStruct
130134 endian :big_and_little
131135 uint32 :p_type
@@ -137,13 +141,14 @@ class ELF64_Phdr < ELFStruct
137141 uint64 :p_memsz
138142 uint64 :p_align
139143 end
140- # Get program header class according to bits.
144+
145+ # Gets the class of program header according to bits.
141146 ELF_Phdr = {
142147 32 => ELF32_Phdr ,
143148 64 => ELF64_Phdr
144149 } . freeze
145150
146- # Symbol structure for 32bit .
151+ # Symbol structure for 32-bit .
147152 class ELF32_sym < ELFStruct
148153 endian :big_and_little
149154 uint32 :st_name
@@ -154,7 +159,7 @@ class ELF32_sym < ELFStruct
154159 uint16 :st_shndx
155160 end
156161
157- # Symbol structure for 64bit .
162+ # Symbol structure for 64-bit .
158163 class ELF64_sym < ELFStruct
159164 endian :big_and_little
160165 uint32 :st_name # Symbol name, index in string tbl
@@ -164,6 +169,7 @@ class ELF64_sym < ELFStruct
164169 uint64 :st_value # Value of the symbol
165170 uint64 :st_size # Associated symbol size
166171 end
172+
167173 # Get symbol header class according to bits.
168174 ELF_sym = {
169175 32 => ELF32_sym ,
0 commit comments