1 /* 2 * ELF payload decoding support. 3 * 4 * Copyright (C) 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <exec/segment.h> 23 24 25 26 /* Generic program segment interface. */ 27 28 class ProgramSegment 29 { 30 public: 31 virtual bool loadable() = 0; 32 virtual offset_t file_contents() = 0; 33 virtual offset_t file_offset() = 0; 34 virtual l4_addr_t region_address() = 0; 35 virtual offset_t region_size() = 0; 36 virtual l4re_rm_flags_t region_flags() = 0; 37 38 Segment *segment(); 39 }; 40 41 template <typename PROGRAM_HEADER> 42 class ProgramSegmentVariant : public ProgramSegment 43 { 44 protected: 45 PROGRAM_HEADER *_header; 46 47 public: 48 explicit ProgramSegmentVariant(PROGRAM_HEADER *header); 49 50 bool loadable(); 51 offset_t file_contents(); 52 offset_t file_offset(); 53 l4_addr_t region_address(); 54 offset_t region_size(); 55 l4re_rm_flags_t region_flags(); 56 }; 57 58 59 60 /* Generic interface for an ELF payload. */ 61 62 class Payload 63 { 64 public: 65 virtual l4_addr_t entry_point() = 0; 66 virtual offset_t header_extent() = 0; 67 virtual offset_t program_header_extent() = 0; 68 virtual unsigned int segments() = 0; 69 virtual ProgramSegment *segment(unsigned int i) = 0; 70 }; 71 72 template <typename HEADER, typename PROGRAM_HEADER> 73 class PayloadVariant : public Payload 74 { 75 protected: 76 HEADER *_header; 77 78 public: 79 explicit PayloadVariant(HEADER *header); 80 81 l4_addr_t entry_point(); 82 offset_t header_extent(); 83 offset_t program_header_extent(); 84 unsigned int segments(); 85 ProgramSegment *segment(unsigned int i); 86 }; 87 88 89 90 /* Return the appropriate payload object. */ 91 92 Payload *get_payload(char *buf); 93 94 /* vim: tabstop=2 expandtab shiftwidth=2 95 */