Home | History | Annotate | Download | only in linker
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in
     12  *    the documentation and/or other materials provided with the
     13  *    distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include "private/KernelArgumentBlock.h"
     30 
     31 extern const char linker_code_start;
     32 extern const char original_start;
     33 extern const char linker_entry;
     34 
     35 /*
     36  * This is the entry point for the linker wrapper, which finds
     37  * the real linker, then bootstraps into it.
     38  */
     39 extern "C" ElfW(Addr) __linker_init(void* raw_args) {
     40   KernelArgumentBlock args(raw_args);
     41 
     42   static uintptr_t linker_offset = reinterpret_cast<uintptr_t>(&linker_code_start);
     43   static uintptr_t linktime_addr = reinterpret_cast<uintptr_t>(&linktime_addr);
     44   ElfW(Addr) my_addr = reinterpret_cast<uintptr_t>(&linktime_addr) - linktime_addr;
     45 
     46   // Set AT_ENTRY to the proper entry point
     47   for (ElfW(auxv_t)* v = args.auxv; v->a_type != AT_NULL; ++v) {
     48     if (v->a_type == AT_BASE) {
     49       v->a_un.a_val = my_addr + linker_offset;
     50     }
     51     if (v->a_type == AT_ENTRY) {
     52       v->a_un.a_val = my_addr + reinterpret_cast<uintptr_t>(&original_start);
     53     }
     54   }
     55 
     56   // Return address of linker entry point -- may need to ensure that raw_args
     57   // was saved.
     58   return my_addr + linker_offset + reinterpret_cast<uintptr_t>(&linker_entry);
     59 }
     60