1 // 2 // Copyright (c) 2010-2018 Antmicro 3 // 4 // This file is licensed under the MIT License. 5 // Full license text is available in 'licenses/MIT.txt'. 6 // 7 using System; 8 using ELFSharp.ELF; 9 using ELFSharp.ELF.Sections; 10 using ELFSharp.ELF.Segments; 11 12 namespace Antmicro.Renode.Peripherals.Bus 13 { 14 public static class ELFExtensions 15 { GetBitness(this IELF elf)16 public static int GetBitness(this IELF elf) 17 { 18 if(elf is ELF<uint>) 19 { 20 return 32; 21 } 22 if(elf is ELF<ulong>) 23 { 24 return 64; 25 } 26 27 throw new ArgumentException(ExceptionMessage); 28 } 29 GetEntryPoint(this IELF elf)30 public static ulong GetEntryPoint(this IELF elf) 31 { 32 if(elf is ELF<uint> elf32) 33 { 34 return elf32.EntryPoint; 35 } 36 if(elf is ELF<ulong> elf64) 37 { 38 return elf64.EntryPoint; 39 } 40 41 throw new ArgumentException(ExceptionMessage); 42 } 43 GetSegmentAddress(this ISegment segment)44 public static ulong GetSegmentAddress(this ISegment segment) 45 { 46 if(segment is Segment<uint> segment32) 47 { 48 return segment32.Address; 49 } 50 if(segment is Segment<ulong> segment64) 51 { 52 return segment64.Address; 53 } 54 55 throw new ArgumentException(ExceptionMessage); 56 } 57 GetSegmentPhysicalAddress(this ISegment segment)58 public static ulong GetSegmentPhysicalAddress(this ISegment segment) 59 { 60 if(segment is Segment<uint> segment32) 61 { 62 return segment32.PhysicalAddress; 63 } 64 if(segment is Segment<ulong> segment64) 65 { 66 return segment64.PhysicalAddress; 67 } 68 69 throw new ArgumentException(ExceptionMessage); 70 } 71 GetSegmentSize(this ISegment segment)72 public static ulong GetSegmentSize(this ISegment segment) 73 { 74 if(segment is Segment<uint> segment32) 75 { 76 return segment32.Size; 77 } 78 if(segment is Segment<ulong> segment64) 79 { 80 return segment64.Size; 81 } 82 83 throw new ArgumentException(ExceptionMessage); 84 } 85 GetSectionPhysicalAddress(this ISection section)86 public static ulong GetSectionPhysicalAddress(this ISection section) 87 { 88 if(section is Section<uint> section32) 89 { 90 return section32.LoadAddress; 91 } 92 if(section is Section<ulong> section64) 93 { 94 return section64.LoadAddress; 95 } 96 97 throw new ArgumentException(ExceptionMessage); 98 } 99 100 private const string ExceptionMessage = "Unsupported ELF format - only 32 and 64-bit ELFs are supported"; 101 } 102 } 103