Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 60593937 authored by Pawan Wagh's avatar Pawan Wagh
Browse files

Separate out ELF header reading function

Separate out ELF header reading code to be used for
alignment checks

Test: atest FileSystemUtilsTests
Bug: 371049373
Change-Id: I8179b8caa0d15d8fadb7b9f307b0d860df30b8b3
parent 9fd0c6a7
Loading
Loading
Loading
Loading
+11 −8
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@
#include <android-base/hex.h>
#include <android-base/unique_fd.h>
#include <bionic/macros.h>
#include <elf.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
@@ -204,7 +203,8 @@ bool punchHoles(const char *filePath, const uint64_t offset,
    return true;
}

bool punchHolesInElf64(const char *filePath, const uint64_t offset) {
bool getLoadSegmentPhdrs(const char *filePath, const uint64_t offset,
                         std::vector<Elf64_Phdr> &programHeaders) {
    // Open Elf file
    Elf64_Ehdr ehdr;
    std::ifstream inputStream(filePath, std::ifstream::in);
@@ -227,11 +227,6 @@ bool punchHolesInElf64(const char *filePath, const uint64_t offset) {
    uint64_t programHeaderOffset = ehdr.e_phoff;
    uint16_t programHeaderNum = ehdr.e_phnum;

    IF_ALOGD() {
        ALOGD("Punching holes in file: %s programHeaderOffset: %" PRIu64 " programHeaderNum: %hu",
              filePath, programHeaderOffset, programHeaderNum);
    }

    // if this is a zip file, also consider elf offset inside a file
    uint64_t phOffset;
    if (__builtin_add_overflow(offset, programHeaderOffset, &phOffset)) {
@@ -240,7 +235,6 @@ bool punchHolesInElf64(const char *filePath, const uint64_t offset) {
    }
    inputStream.seekg(phOffset);

    std::vector<Elf64_Phdr> programHeaders;
    for (int headerIndex = 0; headerIndex < programHeaderNum; headerIndex++) {
        Elf64_Phdr header;
        inputStream.read((char *)&header, sizeof(header));
@@ -254,6 +248,15 @@ bool punchHolesInElf64(const char *filePath, const uint64_t offset) {
        programHeaders.push_back(header);
    }

    return true;
}

bool punchHolesInElf64(const char *filePath, const uint64_t offset) {
    std::vector<Elf64_Phdr> programHeaders;
    if (!getLoadSegmentPhdrs(filePath, offset, programHeaders)) {
        ALOGE("Failed to read program headers from ELF file.");
        return false;
    }
    return punchHoles(filePath, offset, programHeaders);
}

+10 −0
Original line number Diff line number Diff line
@@ -15,8 +15,11 @@
 */
#pragma once

#include <elf.h>
#include <sys/types.h>

#include <vector>

namespace android {

/*
@@ -35,4 +38,11 @@ bool punchHolesInElf64(const char* filePath, uint64_t offset);
 */
bool punchHolesInZip(const char* filePath, uint64_t offset, uint16_t extraFieldLen);

/*
 * This function reads program headers from ELF file. ELF can be specified with file path directly
 * or it should be at offset inside Apk. Program headers passed to function is populated.
 */
bool getLoadSegmentPhdrs(const char* filePath, const uint64_t offset,
                         std::vector<Elf64_Phdr>& programHeaders);

} // namespace android
 No newline at end of file