#include "gold.h"
#include <algorithm>
#include <vector>
#include "elfcpp_swap.h"
#include "dwarf.h"
#include "object.h"
#include "parameters.h"
#include "reloc.h"
#include "dwarf_reader.h"
namespace gold {
uint64_t
read_unsigned_LEB_128(const unsigned char* buffer, size_t* len)
{
uint64_t result = 0;
size_t num_read = 0;
unsigned int shift = 0;
unsigned char byte;
do
{
if (num_read >= 64 / 7)
{
gold_warning(_("Unusually large LEB128 decoded, "
"debug information may be corrupted"));
break;
}
byte = *buffer++;
num_read++;
result |= (static_cast<uint64_t>(byte & 0x7f)) << shift;
shift += 7;
}
while (byte & 0x80);
*len = num_read;
return result;
}
int64_t
read_signed_LEB_128(const unsigned char* buffer, size_t* len)
{
int64_t result = 0;
int shift = 0;
size_t num_read = 0;
unsigned char byte;
do
{
if (num_read >= 64 / 7)
{
gold_warning(_("Unusually large LEB128 decoded, "
"debug information may be corrupted"));
break;
}
byte = *buffer++;
num_read++;
result |= (static_cast<uint64_t>(byte & 0x7f) << shift);
shift += 7;
}
while (byte & 0x80);
if ((shift < 8 * static_cast<int>(sizeof(result))) && (byte & 0x40))
result |= -((static_cast<int64_t>(1)) << shift);
*len = num_read;
return result;
}
struct LineStateMachine
{
int file_num;
uint64_t address;
int line_num;
int column_num;
unsigned int shndx;
bool is_stmt;
bool basic_block;
bool end_sequence;
};
static void
ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
{
lsm->file_num = 1;
lsm->address = 0;
lsm->line_num = 1;
lsm->column_num = 0;
lsm->shndx = -1U;
lsm->is_stmt = default_is_stmt;
lsm->basic_block = false;
lsm->end_sequence = false;
}
template<int size, bool big_endian>
Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(Object* object,
off_t read_shndx)
: data_valid_(false), buffer_(NULL), symtab_buffer_(NULL),
directories_(), files_(), current_header_index_(-1)
{
unsigned int debug_shndx;
for (debug_shndx = 0; debug_shndx < object->shnum(); ++debug_shndx)
if (object->section_name(debug_shndx) == ".debug_line")
{
section_size_type buffer_size;
this->buffer_ = object->section_contents(debug_shndx, &buffer_size,
false);
this->buffer_end_ = this->buffer_ + buffer_size;
break;
}
if (this->buffer_ == NULL)
return;
bool got_relocs = false;
for (unsigned int reloc_shndx = 0;
reloc_shndx < object->shnum();
++reloc_shndx)
{
unsigned int reloc_sh_type = object->section_type(reloc_shndx);
if ((reloc_sh_type == elfcpp::SHT_REL
|| reloc_sh_type == elfcpp::SHT_RELA)
&& object->section_info(reloc_shndx) == debug_shndx)
{
got_relocs = this->track_relocs_.initialize(object, reloc_shndx,
reloc_sh_type);
break;
}
}
if (got_relocs)
{
unsigned int symtab_shndx;
for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx)
if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
{
this->symtab_buffer_ = object->section_contents(
symtab_shndx, &this->symtab_buffer_size_, false);
break;
}
if (this->symtab_buffer_ == NULL)
return;
}
this->data_valid_ = true;
this->read_line_mappings(object, read_shndx);
}
template<int size, bool big_endian>
const unsigned char*
Sized_dwarf_line_info<size, big_endian>::read_header_prolog(
const unsigned char* lineptr)
{
uint32_t initial_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
lineptr += 4;
if (initial_length == 0xffffffff)
{
header_.offset_size = 8;
initial_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
lineptr += 8;
}
else
header_.offset_size = 4;
header_.total_length = initial_length;
gold_assert(lineptr + header_.total_length <= buffer_end_);
header_.version = elfcpp::Swap_unaligned<16, big_endian>::readval(lineptr);
lineptr += 2;
if (header_.offset_size == 4)
header_.prologue_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
else
header_.prologue_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
lineptr += header_.offset_size;
header_.min_insn_length = *lineptr;
lineptr += 1;
header_.default_is_stmt = *lineptr;
lineptr += 1;
header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
lineptr += 1;
header_.line_range = *lineptr;
lineptr += 1;
header_.opcode_base = *lineptr;
lineptr += 1;
header_.std_opcode_lengths.reserve(header_.opcode_base + 1);
header_.std_opcode_lengths[0] = 0;
for (int i = 1; i < header_.opcode_base; i++)
{
header_.std_opcode_lengths[i] = *lineptr;
lineptr += 1;
}
return lineptr;
}
template<int size, bool big_endian>
const unsigned char*
Sized_dwarf_line_info<size, big_endian>::read_header_tables(
const unsigned char* lineptr)
{
++this->current_header_index_;
gold_assert(static_cast<int>(this->directories_.size())
== this->current_header_index_);
gold_assert(static_cast<int>(this->files_.size())
== this->current_header_index_);
this->directories_.push_back(std::vector<std::string>(1));
this->files_.push_back(std::vector<std::pair<int, std::string> >(1));
if (*lineptr)
{
int dirindex = 1;
while (*lineptr)
{
const char* dirname = reinterpret_cast<const char*>(lineptr);
gold_assert(dirindex
== static_cast<int>(this->directories_.back().size()));
this->directories_.back().push_back(dirname);
lineptr += this->directories_.back().back().size() + 1;
dirindex++;
}
}
lineptr++;
if (*lineptr)
{
int fileindex = 1;
size_t len;
while (*lineptr)
{
const char* filename = reinterpret_cast<const char*>(lineptr);
lineptr += strlen(filename) + 1;
uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
lineptr += len;
if (dirindex >= this->directories_.back().size())
dirindex = 0;
int dirindexi = static_cast<int>(dirindex);
read_unsigned_LEB_128(lineptr, &len);
lineptr += len;
read_unsigned_LEB_128(lineptr, &len);
lineptr += len;
gold_assert(fileindex
== static_cast<int>(this->files_.back().size()));
this->files_.back().push_back(std::make_pair(dirindexi, filename));
fileindex++;
}
}
lineptr++;
return lineptr;
}
template<int size, bool big_endian>
bool
Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
{
size_t oplen = 0;
size_t templen;
unsigned char opcode = *start;
oplen++;
start++;
if (opcode >= header_.opcode_base)
{
opcode -= header_.opcode_base;
const int advance_address = ((opcode / header_.line_range)
* header_.min_insn_length);
lsm->address += advance_address;
const int advance_line = ((opcode % header_.line_range)
+ header_.line_base);
lsm->line_num += advance_line;
lsm->basic_block = true;
*len = oplen;
return true;
}
switch (opcode)
{
case elfcpp::DW_LNS_copy:
lsm->basic_block = false;
*len = oplen;
return true;
case elfcpp::DW_LNS_advance_pc:
{
const uint64_t advance_address
= read_unsigned_LEB_128(start, &templen);
oplen += templen;
lsm->address += header_.min_insn_length * advance_address;
}
break;
case elfcpp::DW_LNS_advance_line:
{
const uint64_t advance_line = read_signed_LEB_128(start, &templen);
oplen += templen;
lsm->line_num += advance_line;
}
break;
case elfcpp::DW_LNS_set_file:
{
const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
oplen += templen;
lsm->file_num = fileno;
}
break;
case elfcpp::DW_LNS_set_column:
{
const uint64_t colno = read_unsigned_LEB_128(start, &templen);
oplen += templen;
lsm->column_num = colno;
}
break;
case elfcpp::DW_LNS_negate_stmt:
lsm->is_stmt = !lsm->is_stmt;
break;
case elfcpp::DW_LNS_set_basic_block:
lsm->basic_block = true;
break;
case elfcpp::DW_LNS_fixed_advance_pc:
{
int advance_address;
advance_address = elfcpp::Swap_unaligned<16, big_endian>::readval(start);
oplen += 2;
lsm->address += advance_address;
}
break;
case elfcpp::DW_LNS_const_add_pc:
{
const int advance_address = (header_.min_insn_length
* ((255 - header_.opcode_base)
/ header_.line_range));
lsm->address += advance_address;
}
break;
case elfcpp::DW_LNS_extended_op:
{
const uint64_t extended_op_len
= read_unsigned_LEB_128(start, &templen);
start += templen;
oplen += templen + extended_op_len;
const unsigned char extended_op = *start;
start++;
switch (extended_op)
{
case elfcpp::DW_LNE_end_sequence:
lsm->line_num = -1;
lsm->end_sequence = true;
*len = oplen;
return true;
case elfcpp::DW_LNE_set_address:
{
lsm->address = elfcpp::Swap_unaligned<size, big_endian>::readval(start);
typename Reloc_map::const_iterator it
= reloc_map_.find(start - this->buffer_);
if (it != reloc_map_.end())
{
lsm->address += it->second.second;
lsm->shndx = it->second.first;
}
else
{
if (this->input_is_relobj())
this->data_valid_ = false;
}
break;
}
case elfcpp::DW_LNE_define_file:
{
const char* filename = reinterpret_cast<const char*>(start);
templen = strlen(filename) + 1;
start += templen;
uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
oplen += templen;
if (dirindex >= this->directories_.back().size())
dirindex = 0;
int dirindexi = static_cast<int>(dirindex);
read_unsigned_LEB_128(start, &templen);
oplen += templen;
read_unsigned_LEB_128(start, &templen);
oplen += templen;
this->files_.back().push_back(std::make_pair(dirindexi,
filename));
}
break;
}
}
break;
default:
{
for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
{
size_t templen;
read_unsigned_LEB_128(start, &templen);
start += templen;
oplen += templen;
}
}
break;
}
*len = oplen;
return false;
}
template<int size, bool big_endian>
unsigned const char*
Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr,
off_t shndx)
{
struct LineStateMachine lsm;
const unsigned char* lengthstart = buffer_;
if (header_.offset_size == 8)
lengthstart += 12;
else
lengthstart += 4;
while (lineptr < lengthstart + header_.total_length)
{
ResetLineStateMachine(&lsm, header_.default_is_stmt);
while (!lsm.end_sequence)
{
size_t oplength;
bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
if (add_line
&& (shndx == -1U || lsm.shndx == -1U || shndx == lsm.shndx))
{
Offset_to_lineno_entry entry
= { lsm.address, this->current_header_index_,
lsm.file_num, lsm.line_num };
line_number_map_[lsm.shndx].push_back(entry);
}
lineptr += oplength;
}
}
return lengthstart + header_.total_length;
}
template<int size, bool big_endian>
unsigned int
Sized_dwarf_line_info<size, big_endian>::symbol_section(
Object* object,
unsigned int sym,
typename elfcpp::Elf_types<size>::Elf_Addr* value,
bool* is_ordinary)
{
const int symsize = elfcpp::Elf_sizes<size>::sym_size;
gold_assert(sym * symsize < this->symtab_buffer_size_);
elfcpp::Sym<size, big_endian> elfsym(this->symtab_buffer_ + sym * symsize);
*value = elfsym.get_st_value();
return object->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
}
template<int size, bool big_endian>
void
Sized_dwarf_line_info<size, big_endian>::read_relocs(Object* object)
{
if (this->symtab_buffer_ == NULL)
return;
typename elfcpp::Elf_types<size>::Elf_Addr value;
off_t reloc_offset;
while ((reloc_offset = this->track_relocs_.next_offset()) != -1)
{
const unsigned int sym = this->track_relocs_.next_symndx();
bool is_ordinary;
const unsigned int shndx = this->symbol_section(object, sym, &value,
&is_ordinary);
if (is_ordinary && shndx != elfcpp::SHN_UNDEF)
this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
this->track_relocs_.advance(reloc_offset + 1);
}
}
template<int size, bool big_endian>
void
Sized_dwarf_line_info<size, big_endian>::read_line_mappings(Object* object,
off_t shndx)
{
gold_assert(this->data_valid_ == true);
this->read_relocs(object);
while (this->buffer_ < this->buffer_end_)
{
const unsigned char* lineptr = this->buffer_;
lineptr = this->read_header_prolog(lineptr);
lineptr = this->read_header_tables(lineptr);
lineptr = this->read_lines(lineptr, shndx);
this->buffer_ = lineptr;
}
for (typename Lineno_map::iterator it = line_number_map_.begin();
it != line_number_map_.end();
++it)
std::sort(it->second.begin(), it->second.end());
}
template<int size, bool big_endian>
bool
Sized_dwarf_line_info<size, big_endian>::input_is_relobj()
{
return this->symtab_buffer_ != NULL;
}
static std::vector<Offset_to_lineno_entry>::const_iterator
offset_to_iterator(const std::vector<Offset_to_lineno_entry>* offsets,
off_t offset)
{
const Offset_to_lineno_entry lookup_key = { offset, 0, 0, 0 };
std::vector<Offset_to_lineno_entry>::const_iterator it
= std::lower_bound(offsets->begin(), offsets->end(), lookup_key);
if ((it == offsets->begin() && offset < it->offset)
|| it == offsets->end())
return offsets->end();
if (offset == it->offset)
{
while (it != offsets->end()
&& it->offset == offset
&& it->line_num == -1)
++it;
if (it == offsets->end() || it->offset != offset)
return offsets->end();
else
return it;
}
gold_assert(it != offsets->begin());
std::vector<Offset_to_lineno_entry>::const_iterator range_end = it;
--it;
const off_t range_value = it->offset;
while (it != offsets->begin() && (it-1)->offset == range_value)
--it;
for (; it != range_end; ++it)
if (it->line_num != -1)
return it;
return offsets->end();
}
template<int size, bool big_endian>
std::string
Sized_dwarf_line_info<size, big_endian>::do_addr2line(unsigned int shndx,
off_t offset)
{
if (this->data_valid_ == false)
return "";
const std::vector<Offset_to_lineno_entry>* offsets;
if (this->input_is_relobj())
offsets = &this->line_number_map_[shndx];
else
offsets = &this->line_number_map_[-1U];
if (offsets->empty())
return "";
typename std::vector<Offset_to_lineno_entry>::const_iterator it
= offset_to_iterator(offsets, offset);
if (it == offsets->end())
return "";
std::string ret;
gold_assert(it->header_num < static_cast<int>(this->files_.size()));
gold_assert(it->file_num
< static_cast<int>(this->files_[it->header_num].size()));
const std::pair<int, std::string>& filename_pair
= this->files_[it->header_num][it->file_num];
const std::string& filename = filename_pair.second;
gold_assert(it->header_num < static_cast<int>(this->directories_.size()));
gold_assert(filename_pair.first
< static_cast<int>(this->directories_[it->header_num].size()));
const std::string& dirname
= this->directories_[it->header_num][filename_pair.first];
if (!dirname.empty())
{
ret += dirname;
ret += "/";
}
ret += filename;
if (ret.empty())
ret = "(unknown)";
char buffer[64];
snprintf(buffer, sizeof(buffer), "%d", it->line_num);
ret += ":";
ret += buffer;
return ret;
}
static unsigned int next_generation_count = 0;
struct Addr2line_cache_entry
{
Object* object;
unsigned int shndx;
Dwarf_line_info* dwarf_line_info;
unsigned int generation_count;
unsigned int access_count;
Addr2line_cache_entry(Object* o, unsigned int s, Dwarf_line_info* d)
: object(o), shndx(s), dwarf_line_info(d),
generation_count(next_generation_count), access_count(0)
{
if (next_generation_count < (1U << 31))
++next_generation_count;
}
};
static std::vector<Addr2line_cache_entry> addr2line_cache;
std::string
Dwarf_line_info::one_addr2line(Object* object,
unsigned int shndx, off_t offset,
size_t cache_size)
{
Dwarf_line_info* lineinfo = NULL;
std::vector<Addr2line_cache_entry>::iterator it;
for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
{
if (it->object == object && it->shndx == shndx)
{
lineinfo = it->dwarf_line_info;
it->generation_count = next_generation_count;
if (next_generation_count < (1U << 31))
++next_generation_count;
if (it->access_count < 31)
++it->access_count;
break;
}
}
if (lineinfo == NULL)
{
switch (parameters->size_and_endianness())
{
#ifdef HAVE_TARGET_32_LITTLE
case Parameters::TARGET_32_LITTLE:
lineinfo = new Sized_dwarf_line_info<32, false>(object, shndx); break;
#endif
#ifdef HAVE_TARGET_32_BIG
case Parameters::TARGET_32_BIG:
lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break;
#endif
#ifdef HAVE_TARGET_64_LITTLE
case Parameters::TARGET_64_LITTLE:
lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break;
#endif
#ifdef HAVE_TARGET_64_BIG
case Parameters::TARGET_64_BIG:
lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break;
#endif
default:
gold_unreachable();
}
addr2line_cache.push_back(Addr2line_cache_entry(object, shndx, lineinfo));
}
std::string retval = lineinfo->addr2line(shndx, offset);
while (addr2line_cache.size() > cache_size)
{
unsigned int lowest_score = ~0U;
std::vector<Addr2line_cache_entry>::iterator lowest
= addr2line_cache.end();
for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
{
const unsigned int score = (it->generation_count
+ (1U << it->access_count));
if (score < lowest_score)
{
lowest_score = score;
lowest = it;
}
}
if (lowest != addr2line_cache.end())
{
delete lowest->dwarf_line_info;
addr2line_cache.erase(lowest);
}
}
return retval;
}
void
Dwarf_line_info::clear_addr2line_cache()
{
for (std::vector<Addr2line_cache_entry>::iterator it = addr2line_cache.begin();
it != addr2line_cache.end();
++it)
delete it->dwarf_line_info;
addr2line_cache.clear();
}
#ifdef HAVE_TARGET_32_LITTLE
template
class Sized_dwarf_line_info<32, false>;
#endif
#ifdef HAVE_TARGET_32_BIG
template
class Sized_dwarf_line_info<32, true>;
#endif
#ifdef HAVE_TARGET_64_LITTLE
template
class Sized_dwarf_line_info<64, false>;
#endif
#ifdef HAVE_TARGET_64_BIG
template
class Sized_dwarf_line_info<64, true>;
#endif
}