xref: /netbsd-src/external/gpl3/binutils.old/dist/gold/reduced_debug_output.h (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos // reduced_debug_output.h -- reduce debugging information  -*- C++ -*-
275fd0b74Schristos 
3*e992f068Schristos // Copyright (C) 2008-2022 Free Software Foundation, Inc.
475fd0b74Schristos // Written by Caleb Howe <cshowe@google.com>.
575fd0b74Schristos 
675fd0b74Schristos // This file is part of gold.
775fd0b74Schristos 
875fd0b74Schristos // This program is free software; you can redistribute it and/or modify
975fd0b74Schristos // it under the terms of the GNU General Public License as published by
1075fd0b74Schristos // the Free Software Foundation; either version 3 of the License, or
1175fd0b74Schristos // (at your option) any later version.
1275fd0b74Schristos 
1375fd0b74Schristos // This program is distributed in the hope that it will be useful,
1475fd0b74Schristos // but WITHOUT ANY WARRANTY; without even the implied warranty of
1575fd0b74Schristos // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1675fd0b74Schristos // GNU General Public License for more details.
1775fd0b74Schristos 
1875fd0b74Schristos // You should have received a copy of the GNU General Public License
1975fd0b74Schristos // along with this program; if not, write to the Free Software
2075fd0b74Schristos // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
2175fd0b74Schristos // MA 02110-1301, USA.
2275fd0b74Schristos 
2375fd0b74Schristos // Reduce the size of the debug sections by emitting only debug line number
2475fd0b74Schristos // information.  We still need to emit skeleton debug_info and debug_abbrev
2575fd0b74Schristos // sections for standard tools to parse the debug information correctly.  These
2675fd0b74Schristos // classes remove all debug information entries from the .debug_info section
2775fd0b74Schristos // except for those describing compilation units as these DIEs contain
2875fd0b74Schristos // references to the debug line information needed by most parsers.
2975fd0b74Schristos 
3075fd0b74Schristos #ifndef GOLD_REDUCED_DEBUG_OUTPUT_H
3175fd0b74Schristos #define GOLD_REDUCED_DEBUG_OUTPUT_H
3275fd0b74Schristos 
3375fd0b74Schristos #include <map>
3475fd0b74Schristos #include <utility>
3575fd0b74Schristos #include <vector>
3675fd0b74Schristos 
3775fd0b74Schristos #include "output.h"
3875fd0b74Schristos 
3975fd0b74Schristos namespace gold
4075fd0b74Schristos {
4175fd0b74Schristos 
4275fd0b74Schristos class Output_reduced_debug_abbrev_section : public Output_section
4375fd0b74Schristos {
4475fd0b74Schristos  public:
Output_reduced_debug_abbrev_section(const char * name,elfcpp::Elf_Word flags,elfcpp::Elf_Xword type)4575fd0b74Schristos   Output_reduced_debug_abbrev_section(const char* name, elfcpp::Elf_Word flags,
4675fd0b74Schristos 			              elfcpp::Elf_Xword type)
4775fd0b74Schristos     : Output_section(name, flags, type), sized_(false),
4875fd0b74Schristos       abbrev_count_(0), failed_(false)
4975fd0b74Schristos   { this->set_requires_postprocessing(); }
5075fd0b74Schristos 
5175fd0b74Schristos   unsigned char* get_new_abbrev(uint64_t* abbrev_number,
5275fd0b74Schristos                                 uint64_t abbrev_offset);
5375fd0b74Schristos 
5475fd0b74Schristos  protected:
5575fd0b74Schristos   // Set the final data size.
5675fd0b74Schristos   void
5775fd0b74Schristos   set_final_data_size();
5875fd0b74Schristos 
5975fd0b74Schristos   // Write out the new debug abbreviations
6075fd0b74Schristos   void
6175fd0b74Schristos   do_write(Output_file*);
6275fd0b74Schristos 
6375fd0b74Schristos  private:
6475fd0b74Schristos   void
failed(std::string reason)6575fd0b74Schristos   failed(std::string reason)
6675fd0b74Schristos   {
6775fd0b74Schristos     gold_warning("%s", reason.c_str());
6875fd0b74Schristos     failed_ = true;
6975fd0b74Schristos   }
7075fd0b74Schristos 
7175fd0b74Schristos   // The reduced debug abbreviations
7275fd0b74Schristos   std::vector<unsigned char> data_;
7375fd0b74Schristos 
7475fd0b74Schristos   // We map the abbreviation table offset and abbreviation number of the
7575fd0b74Schristos   // old abbreviation to the number and size of the new abbreviation.
7675fd0b74Schristos   std::map<std::pair<uint64_t, uint64_t>,
7775fd0b74Schristos            std::pair<uint64_t, uint64_t> > abbrev_mapping_;
7875fd0b74Schristos 
7975fd0b74Schristos   bool sized_;
8075fd0b74Schristos 
8175fd0b74Schristos   // The count of abbreviations in the output data
8275fd0b74Schristos   int abbrev_count_;
8375fd0b74Schristos 
8475fd0b74Schristos   // Whether or not the debug reduction has failed for any reason
8575fd0b74Schristos   bool failed_;
8675fd0b74Schristos };
8775fd0b74Schristos 
8875fd0b74Schristos class Output_reduced_debug_info_section : public Output_section
8975fd0b74Schristos {
9075fd0b74Schristos  public:
Output_reduced_debug_info_section(const char * name,elfcpp::Elf_Word flags,elfcpp::Elf_Xword type)9175fd0b74Schristos   Output_reduced_debug_info_section(const char* name, elfcpp::Elf_Word flags,
9275fd0b74Schristos 			            elfcpp::Elf_Xword type)
9375fd0b74Schristos     : Output_section(name, flags, type), failed_(false)
9475fd0b74Schristos   { this->set_requires_postprocessing(); }
9575fd0b74Schristos 
9675fd0b74Schristos   void
set_abbreviations(Output_reduced_debug_abbrev_section * abbrevs)9775fd0b74Schristos   set_abbreviations(Output_reduced_debug_abbrev_section* abbrevs)
9875fd0b74Schristos   { associated_abbrev_ = abbrevs; }
9975fd0b74Schristos 
10075fd0b74Schristos  protected:
10175fd0b74Schristos   // Set the final data size.
10275fd0b74Schristos   void
10375fd0b74Schristos   set_final_data_size();
10475fd0b74Schristos 
10575fd0b74Schristos   // Write out the new debug info
10675fd0b74Schristos   void
10775fd0b74Schristos   do_write(Output_file*);
10875fd0b74Schristos 
10975fd0b74Schristos  private:
11075fd0b74Schristos   void
failed(std::string reason)11175fd0b74Schristos   failed(std::string reason)
11275fd0b74Schristos   {
11375fd0b74Schristos     gold_warning("%s", reason.c_str());
11475fd0b74Schristos     this->failed_ = true;
11575fd0b74Schristos   }
11675fd0b74Schristos 
11775fd0b74Schristos   // Given a pointer to the beginning of a die and the beginning of the
11875fd0b74Schristos   // associated abbreviation fills in die_end with the end of the information
11975fd0b74Schristos   // entry.  If successful returns true.  Get_die_end also takes a pointer to
12075fd0b74Schristos   // the end of the buffer containing the die. If die_end would be beyond the
12175fd0b74Schristos   // end of the buffer, or if an unsupported dwarf form is encountered returns
12275fd0b74Schristos   // false.
12375fd0b74Schristos   bool
12475fd0b74Schristos   get_die_end(unsigned char* die, unsigned char* abbrev,
12575fd0b74Schristos 	      unsigned char** die_end, unsigned char* buffer_end,
12675fd0b74Schristos 	      int address_size, bool is64);
12775fd0b74Schristos 
12875fd0b74Schristos   // The reduced debug info
12975fd0b74Schristos   std::vector<unsigned char> data_;
13075fd0b74Schristos 
13175fd0b74Schristos   // Each debug info section needs to be associated with a debug abbrev section
13275fd0b74Schristos   Output_reduced_debug_abbrev_section* associated_abbrev_;
13375fd0b74Schristos 
13475fd0b74Schristos   // Whether or not the debug reduction has failed for any reason
13575fd0b74Schristos   bool failed_;
13675fd0b74Schristos };
13775fd0b74Schristos 
13875fd0b74Schristos } // End namespace gold.
13975fd0b74Schristos 
14075fd0b74Schristos #endif // !defined(GOLD_REDUCED_DEBUG_OUTPUT_H)
141