+static ElfuPhdr* appendPhdr(ElfuElf *me)
+{
+ ElfuPhdr *phdrmp;
+ ElfuPhdr *newmp;
+
+ ELFU_DEBUG("Appending new PHDR\n");
+
+ /* See if we have enough space for more PHDRs. If not, expand
+ * the PHDR they are in. */
+ phdrmp = elfu_mPhdrByOffset(me, me->ehdr.e_phoff);
+ if (!phdrmp) {
+ /* No LOAD maps PHDRs into memory. Let re-layouter move them. */
+ } else {
+ GElf_Off phdr_maxsz = OFFS_END(phdrmp->phdr.p_offset, phdrmp->phdr.p_filesz);
+ ElfuScn *firstms = CIRCLEQ_FIRST(&phdrmp->childScnList);
+
+ /* How much can the PHDR table expand within its LOAD segment? */
+ if (firstms) {
+ phdr_maxsz = MIN(firstms->shdr.sh_offset, phdr_maxsz);
+ }
+ phdr_maxsz -= me->ehdr.e_phoff;
+
+ /* If we don't have enough space, try to make some by expanding
+ * the LOAD segment we are in. There is no other way. */
+ if (phdr_maxsz < (me->ehdr.e_phnum + 1) * me->ehdr.e_phentsize) {
+ ElfuPhdr *mp;
+ ElfuScn *ms;
+ GElf_Word size = ROUNDUP(me->ehdr.e_phentsize, phdrmp->phdr.p_align);
+
+ /* Move our sections */
+ CIRCLEQ_FOREACH(ms, &phdrmp->childScnList, elemChildScn) {
+ if (ms->shdr.sh_offset >= me->ehdr.e_phoff) {
+ ms->shdr.sh_offset += size;
+ }
+ }
+
+ /* Move our PHDRs */
+ CIRCLEQ_FOREACH(mp, &phdrmp->childPhdrList, elemChildPhdr) {
+ if (mp->phdr.p_offset > me->ehdr.e_phoff) {
+ mp->phdr.p_offset += size;
+ } else {
+ mp->phdr.p_vaddr -= size;
+ mp->phdr.p_paddr -= size;
+ }
+
+ if (mp->phdr.p_type == PT_PHDR) {
+ mp->phdr.p_filesz += me->ehdr.e_phentsize;
+ mp->phdr.p_memsz += me->ehdr.e_phentsize;
+ }
+ }
+
+ /* Move other PHDRs and sections */
+ assert(size <= shiftStuffAtAfterOffset(me, me->ehdr.e_phoff + 1, size));
+
+ /* Remap ourselves */
+ phdrmp->phdr.p_vaddr -= size;
+ phdrmp->phdr.p_paddr -= size;
+ phdrmp->phdr.p_filesz += size;
+ phdrmp->phdr.p_memsz += size;
+ }
+ }
+
+ newmp = elfu_mPhdrAlloc();
+ assert(newmp);
+ CIRCLEQ_INSERT_TAIL(&me->phdrList, newmp, elem);
+
+ return newmp;
+}
+
+
+
+
+