Class: Elf::Writer::Layout
- Inherits:
-
Object
- Object
- Elf::Writer::Layout
- Defined in:
- lib/mithril/writer.rb
Defined Under Namespace
Classes: LoadMap
Constant Summary collapse
- RESERVED_PHDRS =
16
Instance Attribute Summary collapse
-
#tbss_size ⇒ Object
Returns the value of attribute tbss_size.
Instance Method Summary collapse
- #[](idx) ⇒ Object
-
#add(*sections) ⇒ Object
Ordering as follows: Fixed.
- #add_with_phdr(sections, type, flags) ⇒ Object
-
#initialize(factory) ⇒ Layout
constructor
A new instance of Layout.
- #pagealign_loadmap(loadmaps) ⇒ Object
- #pin_section(name, size, vaddr) ⇒ Object
-
#shstrtab(buf) ⇒ Object
Last section written, TODO: Move to layout.
- #to_s ⇒ Object
- #write_phdrs(buf, filehdr, load) ⇒ Object
- #write_sections(buf, filehdr) ⇒ Object
Constructor Details
#initialize(factory) ⇒ Layout
Returns a new instance of Layout.
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/mithril/writer.rb', line 66 def initialize(factory) @layout_by_flags = {} @layout = RBTree.new() @shstrtab = StringTable.new() @factory = factory @phdrs = [] #BinData::Array.new(:type => @factory.phdr) @unallocated =[] @sections = [OutputSection.new("", SHT::SHT_NULL, 0,0,0,0,0,0,0,"")] @sections[0].index = 0 @pinned_sections = {} # Name to vaddr end |
Instance Attribute Details
#tbss_size ⇒ Object
Returns the value of attribute tbss_size.
162 163 164 |
# File 'lib/mithril/writer.rb', line 162 def tbss_size @tbss_size end |
Instance Method Details
#[](idx) ⇒ Object
85 86 87 |
# File 'lib/mithril/writer.rb', line 85 def [](idx) @sections[idx] end |
#add(*sections) ⇒ Object
Ordering as follows: Fixed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/mithril/writer.rb', line 93 def add(*sections) #Ordering as follows: Fixed #(non-nil vaddrs) go where they have to go # Flexible sections are added to lowest hole after section of # the same type retval = [] return [] if sections.empty? sections.each{|sect| sect.index = @sections.size expect_value "Correct size", sect.data.bytesize, sect.siz @sections << sect retval << sect.index } flags = sections.first.flags @layout_by_flags[flags] ||= RBTree.new() if sections.length > 1 || sections.first.vaddr.nil? sections.each { |i| expect_value "All adjacent sections have the same flags", i.flags,flags expect_value "All sections must float", i.vaddr, nil } if sections.length == 1 && @pinned_sections.include?(sections.first.name) sections.first.vaddr = @pinned_sections[sections.first.name] @layout.delete sections.first.vaddr @pinned_sections.delete @sections.first.name else allocate_sections(sections) end end #TODO: Handle the damn nobits if(flags & SHF::SHF_ALLOC == 0) sections.each {|i| @unallocated << i} else sections.each {|i| expect_value "Sections shouldn't overlap", range_available?(@layout,i.vaddr,i.end), true @layout[i.vaddr] = i @layout_by_flags[i.flags][i.vaddr] = i } end retval end |
#add_with_phdr(sections, type, flags) ⇒ Object
133 134 135 136 137 138 |
# File 'lib/mithril/writer.rb', line 133 def add_with_phdr(sections,type,flags) raise RuntimeError.new "need one section in phdr" if sections.empty? x=self.add *sections @phdrs << [sections,type,flags] x end |
#pagealign_loadmap(loadmaps) ⇒ Object
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/mithril/writer.rb', line 152 def pagealign_loadmap(loadmaps) #TODO: Be smarter about this.. loadmaps.sort_by(&:vaddr).each_cons(2) do |low,high| if rounddown(low.vaddr + low.memsz,PAGESIZE) >= rounddown(high.vaddr,PAGESIZE) low.flags |= high.flags high.flags |= low.flags end end loadmaps end |
#pin_section(name, size, vaddr) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/mithril/writer.rb', line 78 def pin_section(name,size,vaddr) x = OutputSection.new(".pin_dummy",SHT::SHT_NULL,0,vaddr,0,0,0,0,0, BinData::Array.new(type: :int8, initial_length: size).to_binary_s) raise RuntimeError.new "Invalid pin" unless range_available?(@layout,vaddr,vaddr+size) @layout[vaddr] = x @pinned_sections[name] = vaddr end |
#shstrtab(buf) ⇒ Object
Last section written, TODO: Move to layout
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/mithril/writer.rb', line 139 def shstrtab(buf) # Last section written, TODO: Move to layout name = ".shstrtab" idx = @shstrtab.add_string name x=@factory.shdr.new x.name= idx x.type = SHT::SHT_STRTAB x.off = buf.tell x.siz = @shstrtab.buf.size buf.write @shstrtab.buf.string x end |
#to_s ⇒ Object
88 89 90 91 92 |
# File 'lib/mithril/writer.rb', line 88 def to_s out = "Layout\n" @layout.each {|key,value| out << "#{key.to_s(16)} #{value.end.to_s(16)} #{value.name}\n"} out end |
#write_phdrs(buf, filehdr, load) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/mithril/writer.rb', line 163 def write_phdrs(buf,filehdr,load) phdrs = BinData::Array.new(:type => @factory.phdr) expect_value "Too many PHDRS",true, @phdrs.size + load.size < RESERVED_PHDRS @phdrs.each {|x| sections, type,flags =*x x = @factory.phdr.new x.align = sections.map(&:align).max x.vaddr = sections.map(&:vaddr).min x.paddr = x.vaddr x.filesz = sections.map(&:end).max - x.vaddr if type == PT::PT_TLS and @tbss_size #HACK x.memsz = x.filesz + @tbss_size else x.memsz = x.filesz end x.flags = flags x.off = sections.map(&:off).min x.type = type phdrs.push x } # load.first.andand.tap{|l| l.vaddr -= l.off l.memsz += l.off l.off = 0 } pagealign_loadmap(load) load.each {|l| phdrs.push @factory.phdr.new.tap {|x| x.align = [PAGESIZE,l.align].max x.vaddr = l.vaddr x.paddr = x.vaddr x.type = PT::PT_LOAD x.filesz = l.end - l.off#TODO: handle nobits x.memsz = x.filesz # l.memsz x.off = l.off x.flags = PF::PF_R x.flags |= PF::PF_W if(l.flags & SHF::SHF_WRITE != 0) x.flags |= PF::PF_X if(l.flags & SHF::SHF_EXECINSTR != 0) } } #phdrs.unshift @factory.phdr.new.tap {|x| # x.align = 4 # x.vaddr = # x.type = PT::PT_PHDRS #} # pp load filehdr.phoff = buf.tell filehdr.phentsize = @factory.phdr.new.num_bytes filehdr.phnum = phdrs.size phdrs.write buf end |
#write_sections(buf, filehdr) ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/mithril/writer.rb', line 219 def write_sections(buf,filehdr) first_shdr = @sections.first first_shdr.index = 0 #Get more clever about mapping files # We put actual program headers right at the beginning. phdr_off = buf.tell buf.seek phdr_off + RESERVED_PHDRS * @factory.phdr.new.num_bytes offset = buf.tell @sections[0].off = 0 (@layout.to_a.sort_by(&:first).map(&:last) + @unallocated).each {|s| if s.flags & SHF::SHF_ALLOC != 0 offset = align_mod(offset,s.vaddr, PAGESIZE) end s.off = offset offset += s.data.bytesize } idx = 0 @sections.each{|s| expect_value "Size field correct", s.siz, s.data.bytesize} @sections.sort_by(&:off).each_cons(2){|low,high| expect_value "Offsets should not overlap", true, low.off + low.data.bytesize <= high.off } shdrs = BinData::Array.new(:type=>@factory.shdr).push *@sections.map{ |s| expect_value "aligned to vaddr", 0,s.vaddr % s.align if s.align != 0 expect_value "idx", idx,s.index idx +=1 #expect_value "aligned to pagesize",0, PAGESIZE % s.align buf.seek s.off old_off = buf.tell buf.write(s.data) # pp "#{idx }#{s.name} written to offset #{old_off} to #{buf.tell}" expect_value "size", s.data.bytesize, s.siz link_value = lambda do |name| if name.is_a? String @sections.to_enum.with_index.select {|sect,idx| sect.name == name}.first.last rescue raise RuntimeError.new("Invalid Section reference #{name}") #Index of first match TODO: check unique else name || 0 end end x= @factory.shdr.new x.name = @shstrtab.add_string(s.name) x.type = s.type x.flags = s.flags x.vaddr = s.vaddr x.off = s.off x.siz = s.siz x.link = link_value.call(s.link) x.info = link_value.call(s.info) x.addralign = s.align x.entsize= s.entsize # x.flags |= SHF::SHF_ALLOC if(@file.gnu_tls.tbss == s) x } #remove mapped = @sections.select{|x| x.flags & SHF::SHF_ALLOC != 0}.sort_by(&:vaddr) mapped.each_cons(2){|low,high| expect_value "Mapped sections should not overlap", true,low.vaddr + low.siz<= high.vaddr} load = mapped.group_by(&:flags).map{|flags,sections| sections.group_by{|x| x.vaddr - x.off}.map do |align,sections| memsize = sections.last.off + sections.last.siz - sections.first.off # if sections.last.type == SHT::SHT_ end # sections.select{|x| x.type == SHT::SHT_NOBITS}.tap {|nobits| # if nobits.size > 0 # expect_value "At most one NOBITS", nobits.size,1 # expect_value "NOBITS is the last section",sections.last.type, SHT::SHT_NOBITS # last_file_addr = nobits.first.vaddr # memsize = sections.last.off - sections.first.off # end # } LoadMap.new(sections.first.off, sections.last.off + sections.last.siz, sections.first.vaddr, memsize ,flags,sections.map(&:align).max) #TODO: LCM? end }.flatten buf.seek 0,IO::SEEK_END shdrs << shstrtab(buf) filehdr.shstrndx = shdrs.size - 1 filehdr.shoff = buf.tell filehdr.shentsize = shdrs[0].num_bytes filehdr.shnum = shdrs.size shdrs.write buf buf.seek roundup(buf.tell, PAGESIZE)-1 buf.write '\0' # pad to pagesize buf.seek phdr_off write_phdrs(buf,filehdr,load) end |