aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/libc_compat/ubuntu_14/glibc.cpp
blob: 1cc444bce1fdea004829828e4bcf9734aaee1102 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "glibc.h"
#include "features.h"

namespace {
    void ReadAuxVector(Elf64_auxv_t** begin, Elf64_auxv_t** end) noexcept {
        int fd = open("/proc/self/auxv", O_RDONLY | O_CLOEXEC);
        if (fd == -1) {
            return;
        }

        constexpr size_t item_size = sizeof(Elf64_auxv_t);
        constexpr size_t block_size = item_size * 32;

        size_t bytes_read = 0;
        size_t size = 0;

        struct TBuffer {
            ~TBuffer() {
                free(Pointer);
            }
            char* Pointer = nullptr;
        } buffer;

        while (true) {
            size_t bytes_left = size - bytes_read;

            if (!bytes_left) {
                size += block_size;
                char* new_buffer = (char*)realloc(buffer.Pointer, size);
                if (!new_buffer) {
                    return;
                }
                buffer.Pointer = new_buffer;
                continue;
            }

            ssize_t r = read(fd, buffer.Pointer + bytes_read, bytes_left);
            if (!r) {
                break;
            } else if (r < 0) {
                if (errno == EINTR) {
                    continue;
                } else {
                    return;
                }
            }

            bytes_read += r;
        }

        size_t item_count = bytes_read / item_size;
        *begin = (Elf64_auxv_t*)buffer.Pointer;
        *end = (Elf64_auxv_t*)(buffer.Pointer + item_count * item_size);
        buffer.Pointer = nullptr;
    }
}

extern "C" {
    weak unsigned long __getauxval(unsigned long item);
}

namespace NUbuntuCompat {

    TGlibc::TGlibc() noexcept
        : AuxVectorBegin(nullptr)
        , AuxVectorEnd(nullptr)
    {
        if (!__getauxval) {
            ReadAuxVector((Elf64_auxv_t**)&AuxVectorBegin, (Elf64_auxv_t**)&AuxVectorEnd);
        }

        Secure = (bool)GetAuxVal(AT_SECURE);
    }

    TGlibc::~TGlibc() noexcept {
        free(AuxVectorBegin);
    }

    unsigned long TGlibc::GetAuxVal(unsigned long item) noexcept {
        if (__getauxval) {
            return __getauxval(item);
        }

        for (Elf64_auxv_t* p = (Elf64_auxv_t*)AuxVectorBegin; p < (Elf64_auxv_t*)AuxVectorEnd; ++p) {
            if (p->a_type == item) {
                return p->a_un.a_val;
            }
        }

        errno = ENOENT;
        return 0;
    }

    bool TGlibc::IsSecure() noexcept {
        return Secure;
    }

    static TGlibc __attribute__((__init_priority__(101))) GlibcInstance;

    TGlibc& GetGlibc() noexcept {
        return GlibcInstance;
    }
}