+/* This file is part of centaur.
+ *
+ * centaur is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License 2 as
+ * published by the Free Software Foundation.
+
+ * centaur is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with centaur. If not, see <http://www.gnu.org/licenses/>.
+ */
+
#include <assert.h>
#include <stdlib.h>
#include <string.h>
}
-static int appendData(ElfuScn *ms, void *buf, size_t len)
-{
- char *newbuf;
-
- assert(ms);
- assert(ms->shdr.sh_type != SHT_NOBITS);
- assert(ms->databuf);
-
- newbuf = realloc(ms->databuf, ms->shdr.sh_size + len);
- if (!newbuf) {
- ELFU_WARN("appendData: malloc() failed for newbuf.\n");
- return 1;
- }
-
- ms->databuf = newbuf;
- memcpy(newbuf + ms->shdr.sh_size, buf, len);
- ms->shdr.sh_size += len;
- assert(ms->shdr.sh_size == ms->shdr.sh_size);
-
- return 0;
-}
-
static ElfuScn* insertSection(ElfuElf *me, ElfuElf *mrel, ElfuScn *oldscn)
{
/* Inject name */
if (me->shstrtab) {
- char *newname;
size_t newnamelen;
newnamelen = strlen("reladd") + 1;
newnamelen += strlen(elfu_mScnName(mrel, oldscn));
}
- newname = malloc(newnamelen);
+ char newname[newnamelen];
+
strcpy(newname, "reladd");
strcat(newname, elfu_mScnName(mrel, oldscn));
- if (!newname) {
- ELFU_WARN("insertSection: malloc() failed for newname. Leaving section name empty.\n");
- newscn->shdr.sh_name = 0;
- } else {
- size_t offset = me->shstrtab->shdr.sh_size;
-
- if (!appendData(me->shstrtab, newname, newnamelen)) {
- newscn->shdr.sh_name = offset;
- }
+ newscn->shdr.sh_name = me->shstrtab->shdr.sh_size;
- free(newname);
+ if (elfu_mScnAppendData(me->shstrtab, newname, newnamelen)) {
+ newscn->shdr.sh_name = 0;
}
}
ERROR:
if (newscn) {
- // TODO: Destroy newscn
+ elfu_mScnDestroy(newscn);
}
return NULL;
}
}
}
- // TODO: Allocate symtab if none present
- assert(me->symtab);
+ /* If we don't have a symbol table, create one so we have somewhere to
+ * write our new symbols to. */
+ elfu_mSymtabAddGlobalDymtabIfNotPresent(me);
/* Allocate memory for the cloned symbol */
newsym = malloc(sizeof(*newsym));
CIRCLEQ_FOREACH(sym, &mrel->symtab->symtab.syms, elem) {
insertSymClone(me, mrel->symtab, sym);
}
+
+ elfu_mSymtabFlatten(me);
}