on the stack. */
#define MAX_TMPBUF 32768
-
/* Helper function to write out fill bytes. */
static int
fill (int fd, off_t pos, size_t len, char *fillbuf, size_t *filledp)
/*@modifies fillbuf, filledp @*/
{
size_t filled = *filledp;
+ size_t fill_len = MIN (len, FILLBUFSIZE);
- if (unlikely (len > filled) && filled < FILLBUFSIZE)
+ if (unlikely (fill_len > filled) && (filled < FILLBUFSIZE))
{
/* Initialize a few more bytes. */
- memset (fillbuf + filled, __libelf_fill_byte, len - filled);
- *filledp = filled = len;
+ memset (fillbuf + filled, __libelf_fill_byte, fill_len - filled);
+ *filledp = filled = fill_len;
}
do
== elf_typesize (LIBELFBITS, ELF_T_PHDR, 1));
/* Write out the program header table. */
- if ((elf->state.ELFW(elf,LIBELFBITS).phdr_flags | elf->flags) & ELF_F_DIRTY)
+ if (elf->state.ELFW(elf,LIBELFBITS).phdr != NULL
+ && (elf->state.ELFW(elf,LIBELFBITS).phdr_flags | elf->flags) & ELF_F_DIRTY)
{
ElfW2(LIBELFBITS,Phdr) tmp_phdr;
ElfW2(LIBELFBITS,Phdr) *out_phdr = elf->state.ELFW(elf,LIBELFBITS).phdr;
}
while ((list = list->next) != NULL);
-
- assert (shdr_data == &shdr_data_begin[shnum]);
+ if (change_bo || elf->state.ELFW(elf,LIBELFBITS).shdr == NULL)
+ assert (shdr_data == &shdr_data_begin[shnum]);
/* Write out the section header table. */
if (shdr_flags & ELF_F_DIRTY
#find-debuginfo.sh - automagically generate debug info and file list
#for inclusion in an rpm spec file.
-LISTFILE=debugfiles.list
-SOURCEFILE=debugsources.list
+if [ -z "$1" ] ; then BUILDDIR="."
+else BUILDDIR=$1
+fi
-touch .debug_saved_mode
+LISTFILE=$BUILDDIR/debugfiles.list
+SOURCEFILE=$BUILDDIR/debugsources.list
+
+touch $BUILDDIR/.debug_saved_mode
echo -n > $SOURCEFILE
# Strip ELF binaries
mkdir -p ${OUTPUTDIR}
echo extracting debug info from $f
#save old mode
- chmod --reference=$f .debug_saved_mode
+ chmod --reference=$f $BUILDDIR/.debug_saved_mode
#make sure we have write perms
chmod u+w $f
/usr/lib/rpm/debugedit -b $RPM_BUILD_DIR -d /usr/src/debug -l $SOURCEFILE $f
- chmod --reference=.debug_saved_mode $f
+ chmod --reference=$BUILDDIR/.debug_saved_mode $f
/usr/lib/rpm/striptofile -g -u -o $OUTPUTDIR $f || :
done
mkdir -p ${RPM_BUILD_ROOT}/usr/src/debug
-(DIR=`pwd`; cd $RPM_BUILD_DIR; LANG=C sort $DIR/$SOURCEFILE -z -u | cpio -pd0m ${RPM_BUILD_ROOT}/usr/src/debug)
+cat $SOURCEFILE | (cd $RPM_BUILD_DIR; LANG=C sort -z -u | cpio -pd0m ${RPM_BUILD_ROOT}/usr/src/debug)
# stupid cpio creates new directories in mode 0700, fixup
find ${RPM_BUILD_ROOT}/usr/src/debug -type d -print0 | xargs -0 chmod a+rx
+/* Needed for libelf */
+#define _FILE_OFFSET_BITS 64
+
#include "system.h"
#include <elf.h>
/* copy elf header: */
gelf_newehdr(out_elf, ehdr.e_ident[EI_CLASS]);
- gelf_update_ehdr(out_elf, &ehdr);
-
- /* Copy program headers: */
- gelf_newphdr(out_elf, ehdr.e_phnum);
-
- for (i = 0; i < ehdr.e_phnum; i++)
- {
- gelf_getphdr (elf, i, &phdr);
- gelf_update_phdr(out_elf, i, &phdr);
- }
+ ehdr.e_phnum = 0;
+ gelf_update_ehdr (out_elf, &ehdr);
section = NULL;
while ((section = elf_nextscn(elf, section)) != NULL)
for (i = 0; i < ehdr.e_phnum; i++)
{
gelf_getphdr (elf, i, &phdr);
- gelf_update_phdr(out_elf, i, &phdr);
+ gelf_update_phdr (out_elf, i, &phdr);
}
/* Copy section headers */
memcpy (out_data->d_buf, data->d_buf, data->d_size);
strcpy (out_data->d_buf + data->d_size, DEBUGLINKNAME);
- section_header.sh_size += out_data->d_size;
+ section_header.sh_size = MAX (section_header.sh_size, out_data->d_off + out_data->d_size);
changed_offsets = 1;
debuglink_name = data->d_size;
}
GElf_Shdr section_header;
GElf_Shdr out_section_header;
Elf_Data *data, *out_data;
+ GElf_Phdr phdr;
unsigned char *section_strtab;
int keep_section;
UnstripInfo *info;
int unstripinfo_name = 0;
+ int i;
info = malloc (sizeof (UnstripInfo));
{
out_data = elf_newdata(out_section);
- out_data->d_buf = data->d_buf;
- out_data->d_type = data->d_type;
- out_data->d_size = data->d_size;
+ if (ehdr.e_shstrndx == elf_ndxscn(section))
+ {
+ out_data->d_size = data->d_size + strlen (UNSTRIPINFONAME) + 1;
+ out_data->d_buf = malloc (out_data->d_size);
+ memcpy (out_data->d_buf, data->d_buf, data->d_size);
+ strcpy (out_data->d_buf + data->d_size, UNSTRIPINFONAME);
+
+ unstripinfo_name = data->d_size;
+ }
+ else
+ {
+ out_data->d_buf = data->d_buf;
+ out_data->d_size = data->d_size;
+ }
out_data->d_off = data->d_off;
+ out_data->d_type = data->d_type;
out_data->d_align = section_header.sh_addralign;
out_data->d_version = data->d_version;
- last_offset = out_data->d_off + out_data->d_size;
- }
- /* Add ".debuglink" to section header strtab */
- if (ehdr.e_shstrndx == elf_ndxscn(section))
- {
- out_data = elf_newdata(out_section);
-
- out_data->d_size = strlen (UNSTRIPINFONAME) + 1;
- out_data->d_buf = UNSTRIPINFONAME;
- out_data->d_off = last_offset;
- out_data->d_align = 0;
- unstripinfo_name = out_data->d_off;
}
}
else if (keep_all_section_headers)
exit (1);
}
- out_elf = elf_begin (out, ELF_C_WRITE, NULL);
+ out_elf = elf_begin (out, ELF_C_WRITE_MMAP, NULL);
if (out_elf == NULL)
{
fprintf (stderr, "Failed to elf_begin output file: %s\n", debugname);
copy_debuginfo_to_file (elf, out_elf);
- elf_update (out_elf, ELF_C_WRITE);
+ if (elf_update (out_elf, ELF_C_WRITE) < 0)
+ {
+ fprintf (stderr, "Failed to write debug file: %s\n", elf_errmsg (elf_errno()));
+ exit (1);
+ }
elf_end (out_elf);
close (out);
strip_to_file (elf, out_elf, debuglink);
- elf_update (out_elf, ELF_C_WRITE);
+ if (elf_update (out_elf, ELF_C_WRITE) < 0)
+ {
+ fprintf (stderr, "Failed to write stripped file: %s\n", elf_errmsg (elf_errno()));
+ exit (1);
+ }
elf_end (out_elf);
close (out);