1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
/* Copyright 2013-2014 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdbool.h>
#include <elf.h>
/* WARNING: This code is used to self-relocate, it cannot have any
* global reference nor TOC reference. It's also called before BSS
* is cleared.
*/
/* Called from head.S, thus no header. */
int relocate(uint64_t offset, struct elf64_dyn *dyn, struct elf64_rela *rela);
/* Note: This code is simplified according to the assumptions
* that our link address is 0 and we are running at the
* target address already.
*/
int relocate(uint64_t offset, struct elf64_dyn *dyn, struct elf64_rela *rela)
{
uint64_t dt_rela = 0;
uint64_t dt_relacount = 0;
unsigned int i;
/* Look for relocation table */
for (; dyn->d_tag != DT_NULL; dyn++) {
if (dyn->d_tag == DT_RELA)
dt_rela = dyn->d_val;
else if (dyn->d_tag == DT_RELACOUNT)
dt_relacount = dyn->d_val;
}
/* If we miss either rela or relacount, bail */
if (!dt_rela || !dt_relacount)
return -1;
/* Check if the offset is consistent */
if ((offset + dt_rela) != (uint64_t)rela)
return -2;
/* Perform relocations */
for (i = 0; i < dt_relacount; i++, rela++) {
uint64_t *t;
if (ELF64_R_TYPE(rela->r_info) != R_PPC64_RELATIVE)
return -3;
t = (uint64_t *)(rela->r_offset + offset);
*t = rela->r_addend + offset;
}
return 0;
}
|