blob: d55f7458b2c58bb9f8e35826c5d12dfd5dc4865f [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_ELF_WRITER_H_
18#define ART_COMPILER_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"
Brian Carlstrom265091e2013-01-30 14:08:26 -080028#include "os.h"
29
Brian Carlstrom700c8d32012-11-05 10:42:02 -080030namespace art {
Brian Carlstrom265091e2013-01-30 14:08:26 -080031
Brian Carlstrom265091e2013-01-30 14:08:26 -080032class ElfFile;
Vladimir Marko10c13562015-11-25 14:33:36 +000033class OutputStream;
34
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
39class ElfWriter {
40 public:
Brian Carlstrom700c8d32012-11-05 10:42:02 -080041 // Looks up information about location of oat file in elf file container.
42 // Used for ImageWriter to perform memory layout.
43 static void GetOatElfInformation(File* file,
Vladimir Marko3fc99032015-05-13 19:06:30 +010044 size_t* oat_loaded_size,
45 size_t* oat_data_offset);
Brian Carlstrom700c8d32012-11-05 10:42:02 -080046
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070047 // Returns runtime oat_data runtime address for an opened ElfFile.
Tong Shen62d1ca32014-09-03 17:24:56 -070048 static uintptr_t GetOatDataAddress(ElfFile* elf_file);
49
50 static bool Fixup(File* file, uintptr_t oat_data_begin);
Brian Carlstrom6a47b9d2013-05-17 10:58:25 -070051
Ian Rogers3d504072014-03-01 09:16:49 -080052 virtual ~ElfWriter() {}
53
Vladimir Marko10c13562015-11-25 14:33:36 +000054 virtual void Start() = 0;
Vladimir Markoaad75c62016-10-03 08:46:48 +000055 virtual void PrepareDynamicSection(size_t rodata_size,
56 size_t text_size,
57 size_t bss_size,
58 size_t bss_roots_offset) = 0;
Vladimir Marko944da602016-02-19 12:27:55 +000059 virtual void PrepareDebugInfo(const ArrayRef<const debug::MethodDebugInfo>& method_infos) = 0;
Vladimir Marko10c13562015-11-25 14:33:36 +000060 virtual OutputStream* StartRoData() = 0;
61 virtual void EndRoData(OutputStream* rodata) = 0;
62 virtual OutputStream* StartText() = 0;
63 virtual void EndText(OutputStream* text) = 0;
Vladimir Marko10c13562015-11-25 14:33:36 +000064 virtual void WriteDynamicSection() = 0;
David Srbeckyc5bfa972016-02-05 15:49:10 +000065 virtual void WriteDebugInfo(const ArrayRef<const debug::MethodDebugInfo>& method_infos) = 0;
Vladimir Marko10c13562015-11-25 14:33:36 +000066 virtual void WritePatchLocations(const ArrayRef<const uintptr_t>& patch_locations) = 0;
67 virtual bool End() = 0;
Brian Carlstrom265091e2013-01-30 14:08:26 -080068
Vladimir Marko131980f2015-12-03 18:29:23 +000069 // Get the ELF writer's stream. This stream can be used for writing data directly
70 // to a section after the section has been finished. When that's done, the user
71 // should Seek() back to the position where the stream was before this operation.
72 virtual OutputStream* GetStream() = 0;
73
Vladimir Marko944da602016-02-19 12:27:55 +000074 // Get the size that the loaded ELF file will occupy in memory.
75 virtual size_t GetLoadedSize() = 0;
76
Vladimir Marko10c13562015-11-25 14:33:36 +000077 protected:
78 ElfWriter() = default;
Brian Carlstrom700c8d32012-11-05 10:42:02 -080079};
80
81} // namespace art
82
Brian Carlstromfc0e3212013-07-17 14:40:12 -070083#endif // ART_COMPILER_ELF_WRITER_H_