Seiya Nuta | f923d9b | 2019-06-21 00:21:50 +0000 | [diff] [blame] | 1 | #include "Object.h" |
| 2 | #include "../llvm-objcopy.h" |
| 3 | |
| 4 | namespace llvm { |
| 5 | namespace objcopy { |
| 6 | namespace macho { |
| 7 | |
| 8 | const SymbolEntry *SymbolTable::getSymbolByIndex(uint32_t Index) const { |
| 9 | assert(Index < Symbols.size() && "invalid symbol index"); |
| 10 | return Symbols[Index].get(); |
| 11 | } |
| 12 | |
Seiya Nuta | 9bbf2a1 | 2019-10-31 13:51:11 +0900 | [diff] [blame] | 13 | SymbolEntry *SymbolTable::getSymbolByIndex(uint32_t Index) { |
| 14 | return const_cast<SymbolEntry *>( |
| 15 | static_cast<const SymbolTable *>(this)->getSymbolByIndex(Index)); |
| 16 | } |
| 17 | |
| 18 | void SymbolTable::removeSymbols( |
| 19 | function_ref<bool(const std::unique_ptr<SymbolEntry> &)> ToRemove) { |
| 20 | Symbols.erase( |
| 21 | std::remove_if(std::begin(Symbols), std::end(Symbols), ToRemove), |
| 22 | std::end(Symbols)); |
| 23 | } |
| 24 | |
Alexander Shaposhnikov | dc046c7 | 2020-02-21 13:18:36 -0800 | [diff] [blame] | 25 | void Object::removeSections(function_ref<bool(const std::unique_ptr<Section> &)> ToRemove) { |
Seiya Nuta | 7f19dd1 | 2019-10-28 15:40:37 +0900 | [diff] [blame] | 26 | for (LoadCommand &LC : LoadCommands) |
| 27 | LC.Sections.erase(std::remove_if(std::begin(LC.Sections), |
| 28 | std::end(LC.Sections), ToRemove), |
| 29 | std::end(LC.Sections)); |
| 30 | } |
| 31 | |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame] | 32 | void Object::addLoadCommand(LoadCommand LC) { |
| 33 | LoadCommands.push_back(std::move(LC)); |
| 34 | } |
| 35 | |
Seiya Nuta | 9e119ad | 2019-12-16 14:05:06 +0900 | [diff] [blame] | 36 | template <typename SegmentType> |
| 37 | static void constructSegment(SegmentType &Seg, |
| 38 | llvm::MachO::LoadCommandType CmdType, |
| 39 | StringRef SegName) { |
| 40 | assert(SegName.size() <= sizeof(Seg.segname) && "too long segment name"); |
| 41 | memset(&Seg, 0, sizeof(SegmentType)); |
| 42 | Seg.cmd = CmdType; |
| 43 | strncpy(Seg.segname, SegName.data(), SegName.size()); |
| 44 | } |
| 45 | |
| 46 | LoadCommand &Object::addSegment(StringRef SegName) { |
| 47 | LoadCommand LC; |
| 48 | if (is64Bit()) |
| 49 | constructSegment(LC.MachOLoadCommand.segment_command_64_data, |
| 50 | MachO::LC_SEGMENT_64, SegName); |
| 51 | else |
| 52 | constructSegment(LC.MachOLoadCommand.segment_command_data, |
| 53 | MachO::LC_SEGMENT, SegName); |
| 54 | |
Alexander Shaposhnikov | dc046c7 | 2020-02-21 13:18:36 -0800 | [diff] [blame] | 55 | LoadCommands.push_back(std::move(LC)); |
Seiya Nuta | 9e119ad | 2019-12-16 14:05:06 +0900 | [diff] [blame] | 56 | return LoadCommands.back(); |
| 57 | } |
| 58 | |
| 59 | /// Extracts a segment name from a string which is possibly non-null-terminated. |
| 60 | static StringRef extractSegmentName(const char *SegName) { |
| 61 | return StringRef(SegName, |
| 62 | strnlen(SegName, sizeof(MachO::segment_command::segname))); |
| 63 | } |
| 64 | |
| 65 | Optional<StringRef> LoadCommand::getSegmentName() const { |
| 66 | const MachO::macho_load_command &MLC = MachOLoadCommand; |
| 67 | switch (MLC.load_command_data.cmd) { |
| 68 | case MachO::LC_SEGMENT: |
| 69 | return extractSegmentName(MLC.segment_command_data.segname); |
| 70 | case MachO::LC_SEGMENT_64: |
| 71 | return extractSegmentName(MLC.segment_command_64_data.segname); |
| 72 | default: |
| 73 | return None; |
| 74 | } |
| 75 | } |
| 76 | |
Seiya Nuta | f923d9b | 2019-06-21 00:21:50 +0000 | [diff] [blame] | 77 | } // end namespace macho |
| 78 | } // end namespace objcopy |
| 79 | } // end namespace llvm |