Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 17 | #ifndef ART_DEX2OAT_LINKER_ELF_WRITER_H_ |
| 18 | #define ART_DEX2OAT_LINKER_ELF_WRITER_H_ |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 19 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 20 | #include <stdint.h> |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 21 | #include <cstddef> |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 22 | #include <string> |
Ian Rogers | 1212a02 | 2013-03-04 10:48:41 -0800 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
David Brazdil | d9c9037 | 2016-09-14 16:53:55 +0100 | [diff] [blame] | 25 | #include "base/array_ref.h" |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 26 | #include "base/macros.h" |
Ian Rogers | 719d1a3 | 2014-03-06 12:13:39 -0800 | [diff] [blame] | 27 | #include "base/mutex.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 28 | #include "base/os.h" |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 29 | #include "debug/debug_info.h" |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 30 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 31 | namespace art { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 32 | |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 33 | class ElfFile; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 34 | |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 35 | namespace debug { |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 36 | struct MethodDebugInfo; |
David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 37 | } // namespace debug |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 38 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 39 | namespace linker { |
| 40 | |
| 41 | class OutputStream; |
| 42 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 43 | class ElfWriter { |
| 44 | public: |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 45 | // Looks up information about location of oat file in elf file container. |
| 46 | // Used for ImageWriter to perform memory layout. |
| 47 | static void GetOatElfInformation(File* file, |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 48 | size_t* oat_loaded_size, |
| 49 | size_t* oat_data_offset); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 50 | |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 51 | // Returns runtime oat_data runtime address for an opened ElfFile. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 52 | static uintptr_t GetOatDataAddress(ElfFile* elf_file); |
| 53 | |
| 54 | static bool Fixup(File* file, uintptr_t oat_data_begin); |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 55 | |
Ian Rogers | 3d50407 | 2014-03-01 09:16:49 -0800 | [diff] [blame] | 56 | virtual ~ElfWriter() {} |
| 57 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 58 | virtual void Start() = 0; |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 59 | // Prepares memory layout of the whole ELF file, and creates dynamic symbols |
| 60 | // which point to specific areas of interest (usually section begin and end). |
| 61 | // This is needed as multi-image needs to know the memory layout of all ELF |
| 62 | // files, before starting to write them. |
| 63 | // This method must be called before calling GetLoadedSize(). |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 64 | virtual void PrepareDynamicSection(size_t rodata_size, |
| 65 | size_t text_size, |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 66 | size_t data_bimg_rel_ro_size, |
Vladimir Marko | aad75c6 | 2016-10-03 08:46:48 +0000 | [diff] [blame] | 67 | size_t bss_size, |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 68 | size_t bss_methods_offset, |
David Srbecky | ec2cdf4 | 2017-12-08 16:21:25 +0000 | [diff] [blame] | 69 | size_t bss_roots_offset, |
| 70 | size_t dex_section_size) = 0; |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 71 | virtual void PrepareDebugInfo(const debug::DebugInfo& debug_info) = 0; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 72 | virtual OutputStream* StartRoData() = 0; |
| 73 | virtual void EndRoData(OutputStream* rodata) = 0; |
| 74 | virtual OutputStream* StartText() = 0; |
| 75 | virtual void EndText(OutputStream* text) = 0; |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 76 | virtual OutputStream* StartDataBimgRelRo() = 0; |
| 77 | virtual void EndDataBimgRelRo(OutputStream* data_bimg_rel_ro) = 0; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 78 | virtual void WriteDynamicSection() = 0; |
David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 79 | virtual void WriteDebugInfo(const debug::DebugInfo& debug_info) = 0; |
David Srbecky | de91fd4 | 2018-07-05 22:27:08 +0100 | [diff] [blame] | 80 | virtual bool StripDebugInfo() = 0; |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 81 | virtual bool End() = 0; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 82 | |
Vladimir Marko | 131980f | 2015-12-03 18:29:23 +0000 | [diff] [blame] | 83 | // Get the ELF writer's stream. This stream can be used for writing data directly |
| 84 | // to a section after the section has been finished. When that's done, the user |
| 85 | // should Seek() back to the position where the stream was before this operation. |
| 86 | virtual OutputStream* GetStream() = 0; |
| 87 | |
Vladimir Marko | 944da60 | 2016-02-19 12:27:55 +0000 | [diff] [blame] | 88 | // Get the size that the loaded ELF file will occupy in memory. |
| 89 | virtual size_t GetLoadedSize() = 0; |
| 90 | |
Vladimir Marko | 10c1356 | 2015-11-25 14:33:36 +0000 | [diff] [blame] | 91 | protected: |
| 92 | ElfWriter() = default; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 95 | } // namespace linker |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 96 | } // namespace art |
| 97 | |
Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 98 | #endif // ART_DEX2OAT_LINKER_ELF_WRITER_H_ |