1700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom/*
2700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom * Copyright (C) 2012 The Android Open Source Project
3700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom *
4700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License");
5700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom * you may not use this file except in compliance with the License.
6700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom * You may obtain a copy of the License at
7700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom *
8700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom *      http://www.apache.org/licenses/LICENSE-2.0
9700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom *
10700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom * Unless required by applicable law or agreed to in writing, software
11700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS,
12700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom * See the License for the specific language governing permissions and
14700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom * limitations under the License.
15700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom */
16700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
177452797678c4345d4a9e65b03e00af703c2c5fe0Vladimir Marko#ifndef ART_DEX2OAT_LINKER_ELF_WRITER_H_
187452797678c4345d4a9e65b03e00af703c2c5fe0Vladimir Marko#define ART_DEX2OAT_LINKER_ELF_WRITER_H_
19700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
20265091e581c9f643b37e7966890911f09e223269Brian Carlstrom#include <stdint.h>
21265091e581c9f643b37e7966890911f09e223269Brian Carlstrom#include <cstddef>
226a47b9dc850b903aabefcfab4adb132cb68bba2eBrian Carlstrom#include <string>
231212a022fa5f8ef9585d765b1809521812af882cIan Rogers#include <vector>
241212a022fa5f8ef9585d765b1809521812af882cIan Rogers
25d9c90373d640a5e08072cf469c372e24a8c0fc35David Brazdil#include "base/array_ref.h"
266a47b9dc850b903aabefcfab4adb132cb68bba2eBrian Carlstrom#include "base/macros.h"
27719d1a33f6569864f529e5a3fff59e7bca97aad0Ian Rogers#include "base/mutex.h"
288f4b056427a9d2321e3aa4f21ca8ffb18b3e5ae6David Sehr#include "base/os.h"
2932210b9f8c30e202e275764200315fe26f22f34cDavid Srbecky#include "debug/debug_info.h"
30265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
31700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstromnamespace art {
32265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
33265091e581c9f643b37e7966890911f09e223269Brian Carlstromclass ElfFile;
3410c13565474de2786aad7c2e79757ea250747a15Vladimir Marko
35c5bfa97c47d656b76f297af8abcd5f7502987399David Srbeckynamespace debug {
3610c13565474de2786aad7c2e79757ea250747a15Vladimir Markostruct MethodDebugInfo;
37c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky}  // namespace debug
38700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
397452797678c4345d4a9e65b03e00af703c2c5fe0Vladimir Markonamespace linker {
407452797678c4345d4a9e65b03e00af703c2c5fe0Vladimir Marko
417452797678c4345d4a9e65b03e00af703c2c5fe0Vladimir Markoclass OutputStream;
427452797678c4345d4a9e65b03e00af703c2c5fe0Vladimir Marko
43700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstromclass ElfWriter {
44700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom public:
45700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Looks up information about location of oat file in elf file container.
46700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  // Used for ImageWriter to perform memory layout.
47700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom  static void GetOatElfInformation(File* file,
483fc9903407c6e89ffbbc92ded9e272d9de58e9b6Vladimir Marko                                   size_t* oat_loaded_size,
493fc9903407c6e89ffbbc92ded9e272d9de58e9b6Vladimir Marko                                   size_t* oat_data_offset);
50700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
516a47b9dc850b903aabefcfab4adb132cb68bba2eBrian Carlstrom  // Returns runtime oat_data runtime address for an opened ElfFile.
5262d1ca3182a6cbb921799825f43ad36821233fd7Tong Shen  static uintptr_t GetOatDataAddress(ElfFile* elf_file);
5362d1ca3182a6cbb921799825f43ad36821233fd7Tong Shen
5462d1ca3182a6cbb921799825f43ad36821233fd7Tong Shen  static bool Fixup(File* file, uintptr_t oat_data_begin);
556a47b9dc850b903aabefcfab4adb132cb68bba2eBrian Carlstrom
563d504075f7c1204d581923460754bf6d3714b13fIan Rogers  virtual ~ElfWriter() {}
573d504075f7c1204d581923460754bf6d3714b13fIan Rogers
5810c13565474de2786aad7c2e79757ea250747a15Vladimir Marko  virtual void Start() = 0;
59ec2cdf4286921131a5f9b3ed12060657ec40f636David Srbecky  // Prepares memory layout of the whole ELF file, and creates dynamic symbols
60ec2cdf4286921131a5f9b3ed12060657ec40f636David Srbecky  // which point to specific areas of interest (usually section begin and end).
61ec2cdf4286921131a5f9b3ed12060657ec40f636David Srbecky  // This is needed as multi-image needs to know the memory layout of all ELF
62ec2cdf4286921131a5f9b3ed12060657ec40f636David Srbecky  // files, before starting to write them.
63ec2cdf4286921131a5f9b3ed12060657ec40f636David Srbecky  // This method must be called before calling GetLoadedSize().
64aad75c6d5bfab2dc8e30fc99fafe8cd2dc8b74d8Vladimir Marko  virtual void PrepareDynamicSection(size_t rodata_size,
65aad75c6d5bfab2dc8e30fc99fafe8cd2dc8b74d8Vladimir Marko                                     size_t text_size,
66aad75c6d5bfab2dc8e30fc99fafe8cd2dc8b74d8Vladimir Marko                                     size_t bss_size,
670eb882bfc5d260e8014c26adfda11602065aa5d8Vladimir Marko                                     size_t bss_methods_offset,
68ec2cdf4286921131a5f9b3ed12060657ec40f636David Srbecky                                     size_t bss_roots_offset,
69ec2cdf4286921131a5f9b3ed12060657ec40f636David Srbecky                                     size_t dex_section_size) = 0;
7032210b9f8c30e202e275764200315fe26f22f34cDavid Srbecky  virtual void PrepareDebugInfo(const debug::DebugInfo& debug_info) = 0;
7110c13565474de2786aad7c2e79757ea250747a15Vladimir Marko  virtual OutputStream* StartRoData() = 0;
7210c13565474de2786aad7c2e79757ea250747a15Vladimir Marko  virtual void EndRoData(OutputStream* rodata) = 0;
7310c13565474de2786aad7c2e79757ea250747a15Vladimir Marko  virtual OutputStream* StartText() = 0;
7410c13565474de2786aad7c2e79757ea250747a15Vladimir Marko  virtual void EndText(OutputStream* text) = 0;
7510c13565474de2786aad7c2e79757ea250747a15Vladimir Marko  virtual void WriteDynamicSection() = 0;
7632210b9f8c30e202e275764200315fe26f22f34cDavid Srbecky  virtual void WriteDebugInfo(const debug::DebugInfo& debug_info) = 0;
7710c13565474de2786aad7c2e79757ea250747a15Vladimir Marko  virtual bool End() = 0;
78265091e581c9f643b37e7966890911f09e223269Brian Carlstrom
79131980fc9aeb2b4d03480443e0fb494c76ce03a2Vladimir Marko  // Get the ELF writer's stream. This stream can be used for writing data directly
80131980fc9aeb2b4d03480443e0fb494c76ce03a2Vladimir Marko  // to a section after the section has been finished. When that's done, the user
81131980fc9aeb2b4d03480443e0fb494c76ce03a2Vladimir Marko  // should Seek() back to the position where the stream was before this operation.
82131980fc9aeb2b4d03480443e0fb494c76ce03a2Vladimir Marko  virtual OutputStream* GetStream() = 0;
83131980fc9aeb2b4d03480443e0fb494c76ce03a2Vladimir Marko
84944da603cde59a4277f3bbc31d860a90842a1a2aVladimir Marko  // Get the size that the loaded ELF file will occupy in memory.
85944da603cde59a4277f3bbc31d860a90842a1a2aVladimir Marko  virtual size_t GetLoadedSize() = 0;
86944da603cde59a4277f3bbc31d860a90842a1a2aVladimir Marko
8710c13565474de2786aad7c2e79757ea250747a15Vladimir Marko protected:
8810c13565474de2786aad7c2e79757ea250747a15Vladimir Marko  ElfWriter() = default;
89700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom};
90700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
917452797678c4345d4a9e65b03e00af703c2c5fe0Vladimir Marko}  // namespace linker
92700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom}  // namespace art
93700c8d31733534a3d978b75a03f6f7e177dc7e81Brian Carlstrom
947452797678c4345d4a9e65b03e00af703c2c5fe0Vladimir Marko#endif  // ART_DEX2OAT_LINKER_ELF_WRITER_H_
95