1 // Copyright 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SANDBOXED_API_SANDBOX2_UTIL_MINIELF_H_ 16 #define SANDBOXED_API_SANDBOX2_UTIL_MINIELF_H_ 17 18 #include <cstdint> 19 #include <string> 20 #include <vector> 21 22 #include "absl/status/statusor.h" 23 24 namespace sandbox2 { 25 26 // Minimal implementation of an ELF file parser to read the program interpreter. 27 class ElfFile { 28 public: 29 struct Symbol { 30 uintptr_t address; 31 std::string name; 32 }; 33 34 static constexpr uint32_t kGetInterpreter = 1 << 0; 35 static constexpr uint32_t kLoadSymbols = 1 << 1; 36 static constexpr uint32_t kLoadImportedLibraries = 1 << 2; 37 static constexpr uint32_t kAll = 38 kGetInterpreter | kLoadSymbols | kLoadImportedLibraries; 39 40 static absl::StatusOr<ElfFile> ParseFromFile(const std::string& filename, 41 uint32_t features); 42 file_size()43 int64_t file_size() const { return file_size_; } interpreter()44 const std::string& interpreter() const { return interpreter_; } symbols()45 const std::vector<Symbol>& symbols() const { return symbols_; } imported_libraries()46 const std::vector<std::string>& imported_libraries() const { 47 return imported_libraries_; 48 } position_independent()49 bool position_independent() const { return position_independent_; } 50 51 private: 52 friend class ElfParser; 53 54 bool position_independent_; 55 int64_t file_size_ = 0; 56 std::string interpreter_; 57 std::vector<Symbol> symbols_; 58 std::vector<std::string> imported_libraries_; 59 }; 60 61 } // namespace sandbox2 62 63 #endif // SANDBOXED_API_SANDBOX2_UTIL_MINIELF_H_ 64