build/unix/elfhack/inject.c

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include <stdint.h>
michael@0 6 #include <elf.h>
michael@0 7
michael@0 8 /* The Android NDK headers define those */
michael@0 9 #undef Elf_Ehdr
michael@0 10 #undef Elf_Addr
michael@0 11
michael@0 12 #if defined(__LP64__)
michael@0 13 #define Elf_Ehdr Elf64_Ehdr
michael@0 14 #define Elf_Addr Elf64_Addr
michael@0 15 #else
michael@0 16 #define Elf_Ehdr Elf32_Ehdr
michael@0 17 #define Elf_Addr Elf32_Addr
michael@0 18 #endif
michael@0 19
michael@0 20 extern __attribute__((visibility("hidden"))) void original_init(int argc, char **argv, char **env);
michael@0 21
michael@0 22 extern __attribute__((visibility("hidden"))) Elf32_Rel relhack[];
michael@0 23 extern __attribute__((visibility("hidden"))) Elf_Ehdr elf_header;
michael@0 24
michael@0 25 static inline __attribute__((always_inline))
michael@0 26 void do_relocations(void)
michael@0 27 {
michael@0 28 Elf32_Rel *rel;
michael@0 29 Elf_Addr *ptr, *start;
michael@0 30 for (rel = relhack; rel->r_offset; rel++) {
michael@0 31 start = (Elf_Addr *)((intptr_t)&elf_header + rel->r_offset);
michael@0 32 for (ptr = start; ptr < &start[rel->r_info]; ptr++)
michael@0 33 *ptr += (intptr_t)&elf_header;
michael@0 34 }
michael@0 35 }
michael@0 36
michael@0 37 __attribute__((section(".text._init_noinit")))
michael@0 38 int init_noinit(int argc, char **argv, char **env)
michael@0 39 {
michael@0 40 do_relocations();
michael@0 41 return 0;
michael@0 42 }
michael@0 43
michael@0 44 __attribute__((section(".text._init")))
michael@0 45 int init(int argc, char **argv, char **env)
michael@0 46 {
michael@0 47 do_relocations();
michael@0 48 original_init(argc, argv, env);
michael@0 49 // Ensure there is no tail-call optimization, avoiding the use of the
michael@0 50 // B.W instruction in Thumb for the call above.
michael@0 51 return 0;
michael@0 52 }

mercurial