blob: 1e053a195c81f0d336cd70606904ee90ff8ab62c [file] [log] [blame]
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Elliott Hughes5e62b342018-10-25 11:00:00 -070017#pragma once
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080018
19#include <elf.h>
Elliott Hughes0266ae52014-02-10 17:46:57 -080020#include <link.h>
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080021#include <stdint.h>
22#include <sys/auxv.h>
23
Elliott Hughes8eac9af2014-05-09 19:12:08 -070024#include "private/bionic_macros.h"
25
Elliott Hughes0d787c12013-04-04 13:46:46 -070026struct abort_msg_t;
27
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080028// When the kernel starts the dynamic linker, it passes a pointer to a block
29// of memory containing argc, the argv array, the environment variable array,
30// and the array of ELF aux vectors. This class breaks that block up into its
31// constituents for easy access.
32class KernelArgumentBlock {
33 public:
Elliott Hughes5cec3772018-01-19 15:45:23 -080034 explicit KernelArgumentBlock(void* raw_args) {
Elliott Hughesc6200592013-09-30 18:43:46 -070035 uintptr_t* args = reinterpret_cast<uintptr_t*>(raw_args);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080036 argc = static_cast<int>(*args);
37 argv = reinterpret_cast<char**>(args + 1);
38 envp = argv + argc + 1;
39
Elliott Hughes63fbb232016-01-05 16:29:33 -080040 // Skip over all environment variable definitions to find the aux vector.
41 // The end of the environment block is marked by a NULL pointer.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080042 char** p = envp;
Yi Kong32bc0fc2018-08-02 17:31:13 -070043 while (*p != nullptr) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080044 ++p;
45 }
Elliott Hughes63fbb232016-01-05 16:29:33 -080046 ++p; // Skip the NULL itself.
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080047
Elliott Hughes0266ae52014-02-10 17:46:57 -080048 auxv = reinterpret_cast<ElfW(auxv_t)*>(p);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080049 }
50
51 // Similar to ::getauxval but doesn't require the libc global variables to be set up,
Elliott Hughes63fbb232016-01-05 16:29:33 -080052 // so it's safe to call this really early on.
53 unsigned long getauxval(unsigned long type) {
Elliott Hughes0266ae52014-02-10 17:46:57 -080054 for (ElfW(auxv_t)* v = auxv; v->a_type != AT_NULL; ++v) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080055 if (v->a_type == type) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080056 return v->a_un.a_val;
57 }
58 }
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080059 return 0;
60 }
61
62 int argc;
63 char** argv;
64 char** envp;
Elliott Hughes0266ae52014-02-10 17:46:57 -080065 ElfW(auxv_t)* auxv;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080066
Josh Gaof6e5b582018-06-01 15:30:54 -070067 // Other data that we want to pass from the dynamic linker to libc.so.
Elliott Hughes0d787c12013-04-04 13:46:46 -070068 abort_msg_t** abort_message_ptr;
69
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080070 private:
Elliott Hughes5e62b342018-10-25 11:00:00 -070071 BIONIC_DISALLOW_COPY_AND_ASSIGN(KernelArgumentBlock);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -080072};