| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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_COMPILER_DEBUG_ELF_GNU_DEBUGDATA_WRITER_H_ |
| 18 | #define ART_COMPILER_DEBUG_ELF_GNU_DEBUGDATA_WRITER_H_ |
| 19 | |
| 20 | #include <vector> |
| 21 | |
| 22 | #include "arch/instruction_set.h" |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 23 | #include "linker/elf_builder.h" |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 24 | #include "linker/vector_output_stream.h" |
| 25 | |
| 26 | // liblzma. |
| 27 | #include "7zCrc.h" |
| 28 | #include "XzCrc64.h" |
| 29 | #include "XzEnc.h" |
| 30 | |
| 31 | namespace art { |
| 32 | namespace debug { |
| 33 | |
| 34 | static void XzCompress(const std::vector<uint8_t>* src, std::vector<uint8_t>* dst) { |
| 35 | // Configure the compression library. |
| 36 | CrcGenerateTable(); |
| 37 | Crc64GenerateTable(); |
| 38 | CLzma2EncProps lzma2Props; |
| 39 | Lzma2EncProps_Init(&lzma2Props); |
| 40 | lzma2Props.lzmaProps.level = 1; // Fast compression. |
| 41 | Lzma2EncProps_Normalize(&lzma2Props); |
| 42 | CXzProps props; |
| 43 | XzProps_Init(&props); |
| Sen Jiang | f9e11ac | 2018-05-04 13:08:48 -0700 | [diff] [blame] | 44 | props.lzma2Props = lzma2Props; |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 45 | // Implement the required interface for communication (written in C so no virtual methods). |
| 46 | struct XzCallbacks : public ISeqInStream, public ISeqOutStream, public ICompressProgress { |
| Sen Jiang | f9e11ac | 2018-05-04 13:08:48 -0700 | [diff] [blame] | 47 | static SRes ReadImpl(const ISeqInStream* p, void* buf, size_t* size) { |
| 48 | auto* ctx = static_cast<XzCallbacks*>(const_cast<ISeqInStream*>(p)); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 49 | *size = std::min(*size, ctx->src_->size() - ctx->src_pos_); |
| 50 | memcpy(buf, ctx->src_->data() + ctx->src_pos_, *size); |
| 51 | ctx->src_pos_ += *size; |
| 52 | return SZ_OK; |
| 53 | } |
| Sen Jiang | f9e11ac | 2018-05-04 13:08:48 -0700 | [diff] [blame] | 54 | static size_t WriteImpl(const ISeqOutStream* p, const void* buf, size_t size) { |
| 55 | auto* ctx = static_cast<const XzCallbacks*>(p); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 56 | const uint8_t* buffer = reinterpret_cast<const uint8_t*>(buf); |
| 57 | ctx->dst_->insert(ctx->dst_->end(), buffer, buffer + size); |
| 58 | return size; |
| 59 | } |
| Sen Jiang | f9e11ac | 2018-05-04 13:08:48 -0700 | [diff] [blame] | 60 | static SRes ProgressImpl(const ICompressProgress* , UInt64, UInt64) { |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 61 | return SZ_OK; |
| 62 | } |
| 63 | size_t src_pos_; |
| 64 | const std::vector<uint8_t>* src_; |
| 65 | std::vector<uint8_t>* dst_; |
| 66 | }; |
| 67 | XzCallbacks callbacks; |
| 68 | callbacks.Read = XzCallbacks::ReadImpl; |
| 69 | callbacks.Write = XzCallbacks::WriteImpl; |
| 70 | callbacks.Progress = XzCallbacks::ProgressImpl; |
| 71 | callbacks.src_pos_ = 0; |
| 72 | callbacks.src_ = src; |
| 73 | callbacks.dst_ = dst; |
| 74 | // Compress. |
| 75 | SRes res = Xz_Encode(&callbacks, &callbacks, &props, &callbacks); |
| 76 | CHECK_EQ(res, SZ_OK); |
| 77 | } |
| 78 | |
| 79 | template <typename ElfTypes> |
| 80 | static std::vector<uint8_t> MakeMiniDebugInfoInternal( |
| 81 | InstructionSet isa, |
| David Srbecky | 5d81120 | 2016-03-08 13:21:22 +0000 | [diff] [blame] | 82 | const InstructionSetFeatures* features, |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 83 | typename ElfTypes::Addr text_section_address, |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 84 | size_t text_section_size, |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 85 | typename ElfTypes::Addr dex_section_address, |
| 86 | size_t dex_section_size, |
| 87 | const DebugInfo& debug_info) { |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 88 | std::vector<uint8_t> buffer; |
| 89 | buffer.reserve(KB); |
| Vladimir Marko | 7452797 | 2016-11-29 15:57:32 +0000 | [diff] [blame] | 90 | linker::VectorOutputStream out("Mini-debug-info ELF file", &buffer); |
| 91 | std::unique_ptr<linker::ElfBuilder<ElfTypes>> builder( |
| 92 | new linker::ElfBuilder<ElfTypes>(isa, features, &out)); |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 93 | builder->Start(false /* write_program_headers */); |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 94 | // Mirror ELF sections as NOBITS since the added symbols will reference them. |
| David Srbecky | f4886df | 2017-12-11 16:06:29 +0000 | [diff] [blame] | 95 | builder->GetText()->AllocateVirtualMemory(text_section_address, text_section_size); |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 96 | if (dex_section_size != 0) { |
| 97 | builder->GetDex()->AllocateVirtualMemory(dex_section_address, dex_section_size); |
| 98 | } |
| 99 | WriteDebugSymbols(builder.get(), true /* mini-debug-info */, debug_info); |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 100 | WriteCFISection(builder.get(), |
| David Srbecky | 32210b9 | 2017-12-04 14:39:21 +0000 | [diff] [blame] | 101 | debug_info.compiled_methods, |
| David Srbecky | c5bfa97 | 2016-02-05 15:49:10 +0000 | [diff] [blame] | 102 | dwarf::DW_DEBUG_FRAME_FORMAT, |
| 103 | false /* write_oat_paches */); |
| 104 | builder->End(); |
| 105 | CHECK(builder->Good()); |
| 106 | std::vector<uint8_t> compressed_buffer; |
| 107 | compressed_buffer.reserve(buffer.size() / 4); |
| 108 | XzCompress(&buffer, &compressed_buffer); |
| 109 | return compressed_buffer; |
| 110 | } |
| 111 | |
| 112 | } // namespace debug |
| 113 | } // namespace art |
| 114 | |
| 115 | #endif // ART_COMPILER_DEBUG_ELF_GNU_DEBUGDATA_WRITER_H_ |