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 
17 #ifndef ART_DEX2OAT_LINKER_ELF_WRITER_H_
18 #define ART_DEX2OAT_LINKER_ELF_WRITER_H_
19 
20 #include <stdint.h>
21 #include <cstddef>
22 #include <string>
23 #include <vector>
24 
25 #include "base/array_ref.h"
26 #include "base/macros.h"
27 #include "base/mutex.h"
28 #include "base/os.h"
29 #include "debug/debug_info.h"
30 
31 namespace art {
32 
33 class ElfFile;
34 class OutputStream;
35 
36 namespace debug {
37 struct MethodDebugInfo;
38 }  // namespace debug
39 
40 namespace linker {
41 
42 class ElfWriter {
43  public:
44   // Looks up information about location of oat file in elf file container.
45   // Used for ImageWriter to perform memory layout.
46   static void GetOatElfInformation(File* file,
47                                    size_t* oat_loaded_size,
48                                    size_t* oat_data_offset);
49 
50   // Returns runtime oat_data runtime address for an opened ElfFile.
51   static uintptr_t GetOatDataAddress(ElfFile* elf_file);
52 
~ElfWriter()53   virtual ~ElfWriter() {}
54 
55   virtual void Start() = 0;
56   // Prepares memory layout of the whole ELF file, and creates dynamic symbols
57   // which point to specific areas of interest (usually section begin and end).
58   // This is needed as multi-image needs to know the memory layout of all ELF
59   // files, before starting to write them.
60   // This method must be called before calling GetLoadedSize().
61   virtual void PrepareDynamicSection(size_t rodata_size,
62                                      size_t text_size,
63                                      size_t data_bimg_rel_ro_size,
64                                      size_t bss_size,
65                                      size_t bss_methods_offset,
66                                      size_t bss_roots_offset,
67                                      size_t dex_section_size) = 0;
68   virtual void PrepareDebugInfo(const debug::DebugInfo& debug_info) = 0;
69   virtual OutputStream* StartRoData() = 0;
70   virtual void EndRoData(OutputStream* rodata) = 0;
71   virtual OutputStream* StartText() = 0;
72   virtual void EndText(OutputStream* text) = 0;
73   virtual OutputStream* StartDataBimgRelRo() = 0;
74   virtual void EndDataBimgRelRo(OutputStream* data_bimg_rel_ro) = 0;
75   virtual void WriteDynamicSection() = 0;
76   virtual void WriteDebugInfo(const debug::DebugInfo& debug_info) = 0;
77   virtual bool StripDebugInfo() = 0;
78   virtual bool End() = 0;
79 
80   // Get the ELF writer's stream. This stream can be used for writing data directly
81   // to a section after the section has been finished. When that's done, the user
82   // should Seek() back to the position where the stream was before this operation.
83   virtual OutputStream* GetStream() = 0;
84 
85   // Get the size that the loaded ELF file will occupy in memory.
86   virtual size_t GetLoadedSize() = 0;
87 
88  protected:
89   ElfWriter() = default;
90 };
91 
92 }  // namespace linker
93 }  // namespace art
94 
95 #endif  // ART_DEX2OAT_LINKER_ELF_WRITER_H_
96