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