GElf_Off injOffset;
ElfuPhdr *injPhdr;
- if (oldscn->shdr.sh_flags & SHF_ALLOC) {
+ if (!(oldscn->shdr.sh_flags & SHF_ALLOC)) {
+ ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %u).\n",
+ elfu_mScnName(mrel, oldscn),
+ oldscn->shdr.sh_type,
+ (unsigned)oldscn->shdr.sh_flags);
+ goto ERROR;
+ } else {
newscn = cloneScn(oldscn);
if (!newscn) {
return NULL;
newscn->shdr.sh_addr = injAddr;
newscn->shdr.sh_offset = injOffset;
+ /* Insert section in child list, ordered by memory address */
if (CIRCLEQ_EMPTY(&injPhdr->childScnList)
- || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_offset < injOffset) {
+ || CIRCLEQ_LAST(&injPhdr->childScnList)->shdr.sh_addr < injAddr) {
CIRCLEQ_INSERT_TAIL(&injPhdr->childScnList, newscn, elemChildScn);
} else {
ElfuScn *ms;
CIRCLEQ_FOREACH(ms, &injPhdr->childScnList, elemChildScn) {
- if (injOffset < ms->shdr.sh_offset) {
+ if (injAddr < ms->shdr.sh_addr) {
CIRCLEQ_INSERT_BEFORE(&injPhdr->childScnList, ms, newscn, elemChildScn);
break;
}
}
return newscn;
- } else {
- ELFU_WARN("insertSection: Skipping non-memory section %s (type %d flags %u).\n",
- elfu_mScnName(mrel, oldscn),
- oldscn->shdr.sh_type,
- (unsigned)oldscn->shdr.sh_flags);
- goto ERROR;
}
ERROR:
case SHT_RELA: /* 4 */
case SHT_REL: /* 9 */
/* Relocate. */
- elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms);
+ if (elfu_mRelocate(me, elfu_mScnByOldscn(me, ms->infoptr), ms)) {
+ return (void*)-1;
+ }
break;
/* The next section types either do not occur in .o files, or are
-void elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
+int elfu_mReladd(ElfuElf *me, const ElfuElf *mrel)
{
assert(me);
assert(mrel);
mergeSymtab(me, mrel);
/* Do relocations and other stuff */
- elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL);
+ if (elfu_mScnForall((ElfuElf*)mrel, subScnAdd2, me, NULL)) {
+ ELFU_WARN("elfu_mReladd: Reladd aborted. Target model is unclean.\n");
+ return -1;
+ }
/* Re-layout to accommodate new contents */
elfu_mLayoutAuto(me);
+
+ return 0;
}