2013-02-07 19:14:39 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-10-25 20:00:00 +02:00
|
|
|
#pragma once
|
2013-02-07 19:14:39 +01:00
|
|
|
|
|
|
|
#include <elf.h>
|
2014-02-11 02:46:57 +01:00
|
|
|
#include <link.h>
|
2013-02-07 19:14:39 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/auxv.h>
|
|
|
|
|
2019-12-20 01:35:51 +01:00
|
|
|
#include "platform/bionic/macros.h"
|
2014-05-10 04:12:08 +02:00
|
|
|
|
2013-02-07 19:14:39 +01:00
|
|
|
// When the kernel starts the dynamic linker, it passes a pointer to a block
|
|
|
|
// of memory containing argc, the argv array, the environment variable array,
|
|
|
|
// and the array of ELF aux vectors. This class breaks that block up into its
|
|
|
|
// constituents for easy access.
|
|
|
|
class KernelArgumentBlock {
|
|
|
|
public:
|
2018-01-20 00:45:23 +01:00
|
|
|
explicit KernelArgumentBlock(void* raw_args) {
|
2013-10-01 03:43:46 +02:00
|
|
|
uintptr_t* args = reinterpret_cast<uintptr_t*>(raw_args);
|
2013-02-07 19:14:39 +01:00
|
|
|
argc = static_cast<int>(*args);
|
|
|
|
argv = reinterpret_cast<char**>(args + 1);
|
|
|
|
envp = argv + argc + 1;
|
|
|
|
|
2016-01-06 01:29:33 +01:00
|
|
|
// Skip over all environment variable definitions to find the aux vector.
|
|
|
|
// The end of the environment block is marked by a NULL pointer.
|
2013-02-07 19:14:39 +01:00
|
|
|
char** p = envp;
|
2018-08-03 02:31:13 +02:00
|
|
|
while (*p != nullptr) {
|
2013-02-07 19:14:39 +01:00
|
|
|
++p;
|
|
|
|
}
|
2016-01-06 01:29:33 +01:00
|
|
|
++p; // Skip the NULL itself.
|
2013-02-07 19:14:39 +01:00
|
|
|
|
2014-02-11 02:46:57 +01:00
|
|
|
auxv = reinterpret_cast<ElfW(auxv_t)*>(p);
|
2013-02-07 19:14:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Similar to ::getauxval but doesn't require the libc global variables to be set up,
|
2016-01-06 01:29:33 +01:00
|
|
|
// so it's safe to call this really early on.
|
|
|
|
unsigned long getauxval(unsigned long type) {
|
2014-02-11 02:46:57 +01:00
|
|
|
for (ElfW(auxv_t)* v = auxv; v->a_type != AT_NULL; ++v) {
|
2013-02-07 19:14:39 +01:00
|
|
|
if (v->a_type == type) {
|
|
|
|
return v->a_un.a_val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int argc;
|
|
|
|
char** argv;
|
|
|
|
char** envp;
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(auxv_t)* auxv;
|
2013-02-07 19:14:39 +01:00
|
|
|
|
|
|
|
private:
|
2018-10-25 20:00:00 +02:00
|
|
|
BIONIC_DISALLOW_COPY_AND_ASSIGN(KernelArgumentBlock);
|
2013-02-07 19:14:39 +01:00
|
|
|
};
|