@@ -261,156 +261,6 @@ void event_object::append_from_elf(const char* fileName) {
261
261
}
262
262
}
263
263
264
- void event_object::append_from_elf (const elf_file& elfFile) {
265
- std::vector<section_data> newSections (elfFile.sections ().size ());
266
- std::vector<bool > outMap (elfFile.sections ().size (), false );
267
-
268
- auto getLocalSymbolName = [this ] (int section, int index) -> std::string {
269
- std::string result;
270
- result.reserve (4 + 8 + 4 + 4 );
271
-
272
- result.append (" _L" );
273
-
274
- util::append_hex (result, size ());
275
- result.append (" _" );
276
- util::append_hex (result, section);
277
- result.append (" _" );
278
- util::append_hex (result, index);
279
-
280
- return result;
281
- };
282
-
283
- auto getGlobalSymbolName = [this ] (const char * name) -> std::string {
284
- std::string result (name);
285
- int find = 0 ;
286
-
287
- while ((find = result.find (' .' )) != std::string::npos)
288
- result[find] = ' _' ;
289
-
290
- return result;
291
- };
292
-
293
- // Initializing written Sections from relevant elf sections
294
-
295
- for (int i=0 ; i<newSections.size (); ++i) {
296
- auto & elfSection = elfFile.sections ()[i];
297
-
298
- if ((elfSection.sh_type == elf::SHT_PROGBITS)) {
299
- if ((elfSection.sh_flags & elf::SHF_ALLOC) && !(elfSection.sh_flags & elf::SHF_WRITE)) {
300
- auto & sectionData = newSections[i];
301
-
302
- sectionData.set_name (elfFile.get_section_name (elfSection));
303
- outMap[i] = true ;
304
- sectionData.load_from_other (elfFile, elfSection.sh_offset , elfSection.sh_size );
305
- }
306
- }
307
- }
308
-
309
- // Initializing labels, mappings & relocations from other elf sections
310
-
311
- for (int sectionIndex=0 ; sectionIndex<elfFile.sections ().size (); ++sectionIndex) {
312
- auto & elfSection = elfFile.sections ()[sectionIndex];
313
-
314
- if (elfSection.sh_type == elf::SHT_SYMTAB) {
315
- int symbolCount = elfSection.sh_size / elfSection.sh_entsize ;
316
-
317
- auto & nameElfSection = elfFile.sections ()[elfSection.sh_link ];
318
-
319
- for (int i=0 ; i<symbolCount; ++i) {
320
- auto sym = elfFile.symbol (elfSection, i);
321
-
322
- if (sym.st_shndx >= newSections.size ()) {
323
- if ((sym.st_shndx == elf::SHN_ABS) && (sym.bind () == elf::STB_GLOBAL))
324
- mAbsoluteSymbols .push_back ({ elfFile.string (nameElfSection, sym.st_name ), sym.st_value , false });
325
- continue ;
326
- }
327
-
328
- if (!outMap[sym.st_shndx ])
329
- continue ;
330
-
331
- auto & sectionData = newSections[sym.st_shndx ];
332
-
333
- std::string symbolName = elfFile.string (nameElfSection, sym.st_name );
334
-
335
- if (sym.type () == elf::STT_NOTYPE && sym.bind () == elf::STB_LOCAL) {
336
- std::string subString = symbolName.substr (0 , 3 );
337
-
338
- if ((symbolName == " $t" ) || (subString == " $t." )) {
339
- sectionData.set_mapping (sym.st_value , mapping::Thumb);
340
- continue ;
341
- } else if ((symbolName == " $a" ) || (subString == " $a." )) {
342
- sectionData.set_mapping (sym.st_value , mapping::ARM);
343
- continue ;
344
- } else if ((symbolName == " $d" ) || (subString == " $d." )) {
345
- sectionData.set_mapping (sym.st_value , mapping::Data);
346
- continue ;
347
- }
348
- }
349
-
350
- if (sym.bind () == elf::STB_LOCAL)
351
- symbolName = getLocalSymbolName (sectionIndex, i);
352
- else
353
- symbolName = getGlobalSymbolName (symbolName.c_str ());
354
-
355
- sectionData.symbols ().push_back ({ symbolName, sym.st_value , (sym.bind () == elf::STB_LOCAL) });
356
- }
357
- } else if (elfSection.sh_type == elf::SHT_REL) {
358
- int relCount = elfSection.sh_size / elfSection.sh_entsize ;
359
-
360
- auto & symbolSection = elfFile.sections ()[elfSection.sh_link ];
361
- auto & symbolNameSection = elfFile.sections ()[symbolSection.sh_link ];
362
-
363
- auto & sectionData = newSections[elfSection.sh_info ];
364
-
365
- for (int i=0 ; i<relCount; ++i) {
366
- auto rel = elfFile.rel (elfSection, i);
367
- auto sym = elfFile.symbol (symbolSection, rel.symId ());
368
- std::string name = elfFile.string (symbolNameSection, sym.st_name );
369
-
370
- if (sym.bind () == elf::STB_LOCAL)
371
- name = getLocalSymbolName (elfSection.sh_link , rel.symId ());
372
- else
373
- name = getGlobalSymbolName (name.c_str ());
374
-
375
- sectionData.relocations ().push_back ({ std::string (name), 0 , rel.type (), rel.r_offset });
376
- }
377
- } else if (elfSection.sh_type == elf::SHT_RELA) {
378
- int relCount = elfSection.sh_size / elfSection.sh_entsize ;
379
-
380
- auto & symbolSection = elfFile.sections ()[elfSection.sh_link ];
381
- auto & symbolNameSection = elfFile.sections ()[symbolSection.sh_link ];
382
-
383
- auto & sectionData = newSections[elfSection.sh_info ];
384
-
385
- for (int i=0 ; i<relCount; ++i) {
386
- auto rela = elfFile.rela (elfSection, i);
387
- auto sym = elfFile.symbol (symbolSection, rela.symId ());
388
- std::string name = elfFile.string (symbolNameSection, sym.st_name );
389
-
390
- if (sym.bind () == elf::STB_LOCAL)
391
- name = getLocalSymbolName (elfSection.sh_link , rela.symId ());
392
- else
393
- name = getGlobalSymbolName (name.c_str ());
394
-
395
- sectionData.relocations ().push_back ({ std::string (name), rela.r_addend , rela.type (), rela.r_offset });
396
- }
397
- }
398
- }
399
-
400
- // Remove empty sections
401
-
402
- newSections.erase (std::remove_if (newSections.begin (), newSections.end (), [] (const section_data& sectionData) {
403
- return (sectionData.size ()==0 );
404
- }), newSections.end ());
405
-
406
- // Create the ultimate lifeform
407
-
408
- for (auto & newSection : newSections) {
409
- combine_with (std::move (newSection));
410
- ensure_aligned (4 );
411
- }
412
- }
413
-
414
264
void event_object::try_transform_relatives () {
415
265
section_data trampolineData;
416
266
0 commit comments