blob: 637330c8357afc7f38a7c79fd7d529430f983b17 [file] [log] [blame]
Brian Carlstrom700c8d32012-11-05 10:42:02 -08001/*
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 Marko74527972016-11-29 15:57:32 +000017#ifndef ART_DEX2OAT_LINKER_ELF_WRITER_H_
18#define ART_DEX2OAT_LINKER_ELF_WRITER_H_
Brian Carlstrom700c8d32012-11-05 10:42:02 -080019
Brian Carlstrom265091e2013-01-30 14:08:26 -080020#include <stdint.h>
Brian Carlstrom265091e2013-01-30 14:08:26 -080021#include <cstddef>
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070022#include <string>
Ian Rogers1212a022013-03-04 10:48:41 -080023#include <vector>
24
David Brazdild9c90372016-09-14 16:53:55 +010025#include "base/array_ref.h"
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070026#include "base/macros.h"
Ian Rogers719d1a32014-03-06 12:13:39 -080027#include "base/mutex.h"
David Sehrc431b9d2018-03-02 12:01:51 -080028#include "base/os.h"
David Srbecky32210b92017-12-04 14:39:21 +000029#include "debug/debug_info.h"
Brian Carlstrom265091e2013-01-30 14:08:26 -080030
Brian Carlstrom700c8d32012-11-05 10:42:02 -080031namespace art {
Brian Carlstrom265091e2013-01-30 14:08:26 -080032
Brian Carlstrom265091e2013-01-30 14:08:26 -080033class ElfFile;
Vladimir Marko10c13562015-11-25 14:33:36 +000034
David Srbeckyc5bfa972016-02-05 15:49:10 +000035namespace debug {
Vladimir Marko10c13562015-11-25 14:33:36 +000036struct MethodDebugInfo;
David Srbeckyc5bfa972016-02-05 15:49:10 +000037} // namespace debug
Brian Carlstrom700c8d32012-11-05 10:42:02 -080038
Vladimir Marko74527972016-11-29 15:57:32 +000039namespace linker {
40
41class OutputStream;
42
Brian Carlstrom700c8d32012-11-05 10:42:02 -080043class ElfWriter {
44 public:
Brian Carlstrom700c8d32012-11-05 10:42:02 -080045 // 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 Marko3fc99032015-05-13 19:06:30 +010048 size_t* oat_loaded_size,
49 size_t* oat_data_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080050
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070051 // Returns runtime oat_data runtime address for an opened ElfFile.
Tong Shen62d1ca32014-09-03 17:24:56 -070052 static uintptr_t GetOatDataAddress(ElfFile* elf_file);
53
54 static bool Fixup(File* file, uintptr_t oat_data_begin);
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070055
Ian Rogers3d504072014-03-01 09:16:49 -080056 virtual ~ElfWriter() {}
57
Vladimir Marko10c13562015-11-25 14:33:36 +000058 virtual void Start() = 0;
David Srbeckyec2cdf42017-12-08 16:21:25 +000059 // 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 Markoaad75c62016-10-03 08:46:48 +000064 virtual void PrepareDynamicSection(size_t rodata_size,
65 size_t text_size,
Vladimir Markob066d432018-01-03 13:14:37 +000066 size_t data_bimg_rel_ro_size,
Vladimir Markoaad75c62016-10-03 08:46:48 +000067 size_t bss_size,
Vladimir Marko0eb882b2017-05-15 13:39:18 +010068 size_t bss_methods_offset,
David Srbeckyec2cdf42017-12-08 16:21:25 +000069 size_t bss_roots_offset,
70 size_t dex_section_size) = 0;
David Srbecky32210b92017-12-04 14:39:21 +000071 virtual void PrepareDebugInfo(const debug::DebugInfo& debug_info) = 0;
Vladimir Marko10c13562015-11-25 14:33:36 +000072 virtual OutputStream* StartRoData() = 0;
73 virtual void EndRoData(OutputStream* rodata) = 0;
74 virtual OutputStream* StartText() = 0;
75 virtual void EndText(OutputStream* text) = 0;
Vladimir Markob066d432018-01-03 13:14:37 +000076 virtual OutputStream* StartDataBimgRelRo() = 0;
77 virtual void EndDataBimgRelRo(OutputStream* data_bimg_rel_ro) = 0;
Vladimir Marko10c13562015-11-25 14:33:36 +000078 virtual void WriteDynamicSection() = 0;
David Srbecky32210b92017-12-04 14:39:21 +000079 virtual void WriteDebugInfo(const debug::DebugInfo& debug_info) = 0;
David Srbeckyde91fd42018-07-05 22:27:08 +010080 virtual bool StripDebugInfo() = 0;
Vladimir Marko10c13562015-11-25 14:33:36 +000081 virtual bool End() = 0;
Brian Carlstrom265091e2013-01-30 14:08:26 -080082
Vladimir Marko131980f2015-12-03 18:29:23 +000083 // 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 Marko944da602016-02-19 12:27:55 +000088 // Get the size that the loaded ELF file will occupy in memory.
89 virtual size_t GetLoadedSize() = 0;
90
Vladimir Marko10c13562015-11-25 14:33:36 +000091 protected:
92 ElfWriter() = default;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080093};
94
Vladimir Marko74527972016-11-29 15:57:32 +000095} // namespace linker
Brian Carlstrom700c8d32012-11-05 10:42:02 -080096} // namespace art
97
Vladimir Marko74527972016-11-29 15:57:32 +000098#endif // ART_DEX2OAT_LINKER_ELF_WRITER_H_