-/* Copyright (C) 1985-1988, 1990, 1992, 1999-2013 Free Software
+/* Copyright (C) 1985-1988, 1990, 1992, 1999-2014 Free Software
Foundation, Inc.
This file is part of GNU Emacs.
Instead we read the whole file, modify it, and write it out. */
#include <config.h>
-#include <unexec.h>
-
-extern _Noreturn void fatal (const char *, ...) ATTRIBUTE_FORMAT_PRINTF (1, 2);
+#include "unexec.h"
+#include "lisp.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <memory.h>
-#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
# define ElfW(type) ElfExpandBitsW (ELFSIZE, type)
#endif
-/* The code often converts ElfW (Half) values like e_shentsize to int;
+/* The code often converts ElfW (Half) values like e_shentsize to ptrdiff_t;
check that this doesn't lose information. */
#include <intprops.h>
#include <verify.h>
-verify ((! TYPE_SIGNED (ElfW (Half)) || INT_MIN <= TYPE_MINIMUM (ElfW (Half)))
- && TYPE_MAXIMUM (ElfW (Half)) <= INT_MAX);
+verify ((! TYPE_SIGNED (ElfW (Half))
+ || PTRDIFF_MIN <= TYPE_MINIMUM (ElfW (Half)))
+ && TYPE_MAXIMUM (ElfW (Half)) <= PTRDIFF_MAX);
#ifdef UNEXELF_DEBUG
# define DEBUG_LOG(expr) fprintf (stderr, #expr " 0x%jx\n", (uintmax_t) (expr))
*/
static void *
-entry_address (void *section_h, int idx, int num, int entsize)
+entry_address (void *section_h, ptrdiff_t idx, ptrdiff_t entsize)
{
char *h = section_h;
- ptrdiff_t n = idx;
- return h + entsize * n;
+ return h + idx * entsize;
}
#define OLD_SECTION_H(n) \
- (*(ElfW (Shdr) *) entry_address (old_section_h, n, old_file_h->e_shnum, \
- old_file_h->e_shentsize))
+ (*(ElfW (Shdr) *) entry_address (old_section_h, n, old_file_h->e_shentsize))
#define NEW_SECTION_H(n) \
- (*(ElfW (Shdr) *) entry_address (new_section_h, n, new_file_h->e_shnum, \
- new_file_h->e_shentsize))
+ (*(ElfW (Shdr) *) entry_address (new_section_h, n, new_file_h->e_shentsize))
#define NEW_PROGRAM_H(n) \
- (*(ElfW (Phdr) *) entry_address (new_program_h, n, new_file_h->e_phnum, \
- new_file_h->e_phentsize))
+ (*(ElfW (Phdr) *) entry_address (new_program_h, n, new_file_h->e_phentsize))
#define PATCH_INDEX(n) ((n) += old_bss_index <= (n))
typedef unsigned char byte;
If we don't find the section NAME, that is a fatal error
if NOERROR is false; return -1 if NOERROR is true. */
-static int
+static ptrdiff_t
find_section (const char *name, const char *section_names, const char *file_name,
ElfW (Ehdr) *old_file_h, ElfW (Shdr) *old_section_h,
bool noerror)
{
- int idx;
+ ptrdiff_t idx;
for (idx = 1; idx < old_file_h->e_shnum; idx++)
{
ElfW (Off) old_bss_offset;
ElfW (Word) new_data2_incr;
- int n, nn;
- int old_bss_index, old_sbss_index, old_plt_index;
- int old_data_index, new_data2_index;
+ ptrdiff_t n, nn;
+ ptrdiff_t old_bss_index, old_sbss_index, old_plt_index;
+ ptrdiff_t old_data_index, new_data2_index;
#if defined _SYSTYPE_SYSV || defined __sgi
- int old_mdebug_index;
+ ptrdiff_t old_mdebug_index;
#endif
struct stat stat_buf;
off_t old_file_size;
+ int mask;
/* Open the old file, allocate a buffer of the right size, and read
in the file contents. */
- old_file = open (old_name, O_RDONLY);
+ old_file = emacs_open (old_name, O_RDONLY, 0);
if (old_file < 0)
fatal ("Can't open %s for reading: %s", old_name, strerror (errno));
fatal ("Can't fstat (%s): %s", old_name, strerror (errno));
#if MAP_ANON == 0
- mmap_fd = open ("/dev/zero", O_RDONLY);
+ mmap_fd = emacs_open ("/dev/zero", O_RDONLY, 0);
if (mmap_fd < 0)
fatal ("Can't open /dev/zero for reading: %s", strerror (errno));
#endif
new_data2_incr = new_data2_size + (new_data2_offset - old_bss_offset);
#ifdef UNEXELF_DEBUG
- fprintf (stderr, "old_bss_index %d\n", old_bss_index);
+ fprintf (stderr, "old_bss_index %td\n", old_bss_index);
DEBUG_LOG (old_bss_addr);
DEBUG_LOG (old_bss_size);
DEBUG_LOG (old_bss_offset);
the image of the new file. Set pointers to various interesting
objects. */
- new_file = open (new_name, O_RDWR | O_CREAT, 0666);
+ new_file = emacs_open (new_name, O_RDWR | O_CREAT, 0666);
if (new_file < 0)
fatal ("Can't creat (%s): %s", new_name, strerror (errno));
#ifdef UNEXELF_DEBUG
DEBUG_LOG (old_file_h->e_shoff);
- fprintf (stderr, "Old section count %d\n", old_file_h->e_shnum);
+ fprintf (stderr, "Old section count %td\n", (ptrdiff_t) old_file_h->e_shnum);
DEBUG_LOG (new_file_h->e_shoff);
- fprintf (stderr, "New section count %d\n", new_file_h->e_shnum);
+ fprintf (stderr, "New section count %td\n", (ptrdiff_t) new_file_h->e_shnum);
#endif
/* Fix up a new program header. Extend the writable data segment so
if (NEW_SECTION_H (nn).sh_type == SHT_MIPS_DEBUG
&& old_mdebug_index != -1)
{
- int diff = NEW_SECTION_H (nn).sh_offset
- - OLD_SECTION_H (old_mdebug_index).sh_offset;
+ ptrdiff_t new_offset = NEW_SECTION_H (nn).sh_offset;
+ ptrdiff_t old_offset = OLD_SECTION_H (old_mdebug_index).sh_offset;
+ ptrdiff_t diff = new_offset - old_offset;
HDRR *phdr = (HDRR *)(NEW_SECTION_H (nn).sh_offset + new_base);
if (diff)
|| NEW_SECTION_H (nn).sh_type == SHT_DYNSYM)
{
ElfW (Shdr) *spt = &NEW_SECTION_H (nn);
- unsigned int num = spt->sh_size / spt->sh_entsize;
+ ptrdiff_t num = spt->sh_size / spt->sh_entsize;
ElfW (Sym) * sym = (ElfW (Sym) *) (NEW_SECTION_H (nn).sh_offset +
new_base);
for (; num--; sym++)
/* Close the files and make the new file executable. */
#if MAP_ANON == 0
- close (mmap_fd);
+ emacs_close (mmap_fd);
#endif
- if (close (old_file) != 0)
+ if (emacs_close (old_file) != 0)
fatal ("Can't close (%s): %s", old_name, strerror (errno));
- if (close (new_file) != 0)
+ if (emacs_close (new_file) != 0)
fatal ("Can't close (%s): %s", new_name, strerror (errno));
if (stat (new_name, &stat_buf) != 0)
fatal ("Can't stat (%s): %s", new_name, strerror (errno));
- n = umask (777);
- umask (n);
- stat_buf.st_mode |= 0111 & ~n;
+ mask = umask (777);
+ umask (mask);
+ stat_buf.st_mode |= 0111 & ~mask;
if (chmod (new_name, stat_buf.st_mode) != 0)
fatal ("Can't chmod (%s): %s", new_name, strerror (errno));
}