/*-
* Copyright (c) 2010 Kai Wang
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* FIXME The logic here is most likely wrong. It should
* be the ISA that determines relocation type.
*/
if (dbg->dbgp_flags & DW_DLC_SIZE_64)
drs->drs_addend = 1;
else
drs->drs_addend = 0;
/*
* If the DW_DLC_SYMBOLIC_RELOCATIONS flag is set or ElfXX_Rel
* is used instead of ELfXX_Rela, we need to write the addend
* in the storage unit to be relocated. Otherwise write 0 in the
* storage unit and the addend will be written into relocation
* section later.
*/
if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) ||
drs->drs_addend == 0)
ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
addend, length, error);
else
ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap, &offset,
0, length, error);
if (ret != DW_DLE_NONE)
return (ret);
if (offset > ds->ds_size)
ds->ds_size = offset;
/*
* Calculate the size (in bytes) of the relocation section.
*/
if (dbg->dbgp_flags & DW_DLC_SIZE_64)
unit = drs->drs_addend ? sizeof(Elf64_Rela) : sizeof(Elf64_Rel);
else
unit = drs->drs_addend ? sizeof(Elf32_Rela) : sizeof(Elf32_Rel);
assert(ds->ds_size == 0);
size = drs->drs_drecnt * unit;
/*
* Discard this relocation section if there is no entry in it.
*/
if (size == 0) {
_dwarf_reloc_section_free(dbg, &drs);
return (DW_DLE_NONE);
}
/*
* If we are under stream mode, realloc the section data block to
* this size.
*/
if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
ds->ds_cap = size;
if ((ds->ds_data = realloc(ds->ds_data, (size_t) ds->ds_cap)) ==
NULL) {
DWARF_SET_ERROR(dbg, error, DW_DLE_MEMORY);
return (DW_DLE_MEMORY);
}
}
/*
* Notify the application the creation of this relocation section.
* Note that the section link here should point to the .symtab
* section, we set it to 0 since we have no way to know .symtab
* section index.
*/
ret = _dwarf_pro_callback(dbg, ds->ds_name, size,
drs->drs_addend ? SHT_RELA : SHT_REL, 0, 0, drs->drs_ref->ds_ndx,
&ds->ds_symndx, NULL);
if (ret < 0) {
DWARF_SET_ERROR(dbg, error, DW_DLE_ELF_SECT_ERR);
return (DW_DLE_ELF_SECT_ERR);
}
ds->ds_ndx = ret;
return (DW_DLE_NONE);
}
int
_dwarf_reloc_section_gen(Dwarf_P_Debug dbg, Dwarf_Rel_Section drs,
Dwarf_Error *error)
{
Dwarf_Rel_Entry dre;
Dwarf_P_Section ds;
Dwarf_Unsigned type;
int ret;
STAILQ_FOREACH(dre, &drs->drs_dre, dre_next) {
assert(dre->dre_length == 4 || dre->dre_length == 8);
type = _dwarf_get_reloc_type(dbg, dre->dre_length == 8);
if (dbg->dbgp_flags & DW_DLC_SIZE_64) {
/* Write r_offset (8 bytes) */
ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
&ds->ds_size, dre->dre_offset, 8, error);
if (ret != DW_DLE_NONE)
return (ret);
/* Write r_info (8 bytes) */
ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
&ds->ds_size, ELF64_R_INFO(dre->dre_symndx, type),
8, error);
if (ret != DW_DLE_NONE)
return (ret);
/* Write r_addend (8 bytes) */
if (drs->drs_addend) {
ret = dbg->write_alloc(&ds->ds_data,
&ds->ds_cap, &ds->ds_size, dre->dre_addend,
8, error);
if (ret != DW_DLE_NONE)
return (ret);
}
} else {
/* Write r_offset (4 bytes) */
ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
&ds->ds_size, dre->dre_offset, 4, error);
if (ret != DW_DLE_NONE)
return (ret);
/* Write r_info (4 bytes) */
ret = dbg->write_alloc(&ds->ds_data, &ds->ds_cap,
&ds->ds_size, ELF32_R_INFO(dre->dre_symndx, type),
4, error);
if (ret != DW_DLE_NONE)
return (ret);
/* Write r_addend (4 bytes) */
if (drs->drs_addend) {
ret = dbg->write_alloc(&ds->ds_data,
&ds->ds_cap, &ds->ds_size, dre->dre_addend,
4, error);
if (ret != DW_DLE_NONE)
return (ret);
}
}
}
assert(ds->ds_size == ds->ds_cap);
return (DW_DLE_NONE);
}
int
_dwarf_reloc_gen(Dwarf_P_Debug dbg, Dwarf_Error *error)
{
Dwarf_Rel_Section drs;
Dwarf_Rel_Entry dre;
Dwarf_P_Section ds;
int ret;
STAILQ_FOREACH(drs, &dbg->dbgp_drslist, drs_next) {
/*
* Update relocation entries: translate any section name
* reference to section symbol index.
*/
STAILQ_FOREACH(dre, &drs->drs_dre, dre_next) {
if (dre->dre_secname == NULL)
continue;
ds = _dwarf_pro_find_section(dbg, dre->dre_secname);
assert(ds != NULL && ds->ds_symndx != 0);
dre->dre_symndx = ds->ds_symndx;
}
/*
* Generate ELF relocation section if we are under stream
* mode.
*/
if ((dbg->dbgp_flags & DW_DLC_SYMBOLIC_RELOCATIONS) == 0) {
ret = _dwarf_reloc_section_gen(dbg, drs, error);
if (ret != DW_DLE_NONE)
return (ret);
}
}