topjohnwu / Magisk

The Magic Mask for Android
GNU General Public License v3.0
47.24k stars 11.96k forks source link

Compilation error when elf_cleaner not exist #2632

Closed zivmc closed 4 years ago

zivmc commented 4 years ago

In clean_elf function of build.py, the command to compile the binary native/out/elf-cleaner missed necessary cxxflags:

         if not os.path.exists(elf_cleaner):
             execv(['g++', 'tools/termux-elf-cleaner/termux-elf-cleaner.cpp',
                   '-o', elf_cleaner])

Lacking of a "-std=c++11" flag caused the following compilation errors:

 Building binaries: magisk magiskinit magiskboot busybox

tools/termux-elf-cleaner/termux-elf-cleaner.cpp: In function ‘bool process_elf(uint8_t*, size_t, const char*)’:
tools/termux-elf-cleaner/termux-elf-cleaner.cpp:77:32: error: ‘nullptr’ was not declared in this scope
     char const* removed_name = nullptr;
                                ^
tools/termux-elf-cleaner/termux-elf-cleaner.cpp:102:50: error: expected ‘;’ before ‘orig_d_val’
      decltype(dynamic_section_entry->d_un.d_val) orig_d_val =
                                                  ^
tools/termux-elf-cleaner/termux-elf-cleaner.cpp:104:50: error: expected ‘;’ before ‘new_d_val’
      decltype(dynamic_section_entry->d_un.d_val) new_d_val =
                                                  ^
tools/termux-elf-cleaner/termux-elf-cleaner.cpp:106:10: error: ‘new_d_val’ was not declared in this scope
      if (new_d_val != orig_d_val) {
          ^
tools/termux-elf-cleaner/termux-elf-cleaner.cpp:106:23: error: ‘orig_d_val’ was not declared in this scope
      if (new_d_val != orig_d_val) {
                       ^
tools/termux-elf-cleaner/termux-elf-cleaner.cpp: In instantiation of ‘bool process_elf(uint8_t*, size_t, const char*) [with ElfHeaderType = Elf32_Ehdr; ElfSectionHeaderType = Elf32_Shdr; ElfDynamicSectionEntryType = Elf32_Dyn; uint8_t = unsigned char; size_t = long unsigned int]’:
tools/termux-elf-cleaner/termux-elf-cleaner.cpp:177:84:   required from here
tools/termux-elf-cleaner/termux-elf-cleaner.cpp:102:14: error: ‘decltype’ was not declared in this scope
      decltype(dynamic_section_entry->d_un.d_val) orig_d_val =
              ^
tools/termux-elf-cleaner/termux-elf-cleaner.cpp:104:14: error: ‘decltype’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
      decltype(dynamic_section_entry->d_un.d_val) new_d_val =
              ^
tools/termux-elf-cleaner/termux-elf-cleaner.cpp:102:14: note: ‘decltype’ declared here, later in the translation unit
      decltype(dynamic_section_entry->d_un.d_val) orig_d_val =
              ^
tools/termux-elf-cleaner/termux-elf-cleaner.cpp: In instantiation of ‘bool process_elf(uint8_t*, size_t, const char*) [with ElfHeaderType = Elf64_Ehdr; ElfSectionHeaderType = Elf64_Shdr; ElfDynamicSectionEntryType = Elf64_Dyn; uint8_t = unsigned char; size_t = long unsigned int]’:
tools/termux-elf-cleaner/termux-elf-cleaner.cpp:179:84:   required from here
tools/termux-elf-cleaner/termux-elf-cleaner.cpp:102:14: error: ‘decltype’ was not declared in this scope
tools/termux-elf-cleaner/termux-elf-cleaner.cpp:104:14: error: ‘decltype’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
      decltype(dynamic_section_entry->d_un.d_val) new_d_val =
              ^
tools/termux-elf-cleaner/termux-elf-cleaner.cpp:102:14: note: ‘decltype’ declared here, later in the translation unit
      decltype(dynamic_section_entry->d_un.d_val) orig_d_val =

A possible fix is to add a "-std=c++11" flag to the compilation command:

        if not os.path.exists(elf_cleaner):
            execv(['g++', '-std=c++11', 'tools/termux-elf-cleaner/termux-elf-cleaner.cpp',
                  '-o', elf_cleaner])
osm0sis commented 4 years ago

How are you compiling this though? Only FrankeNDK is supported, and clearly builds for @topjohnwu and others without error.

zivmc commented 4 years ago

How are you compiling this though? Only FrankeNDK is supported, and clearly builds for @topjohnwu and others without error.

I was compiling on Linux, actually Linux Mint 18.3 based on Ubuntu 16.04. The compilation command was:

./build.py all

It seems that it ran into the clean_elf function:

def clean_elf():
    if is_windows:
        elf_cleaner = os.path.join('tools', 'elf-cleaner.exe')
    else:
        elf_cleaner = os.path.join('native', 'out', 'elf-cleaner')
        if not os.path.exists(elf_cleaner):
            execv(['g++', 'tools/termux-elf-cleaner/termux-elf-cleaner.cpp',
                  '-o', elf_cleaner])
    args = [elf_cleaner]
    args.extend(os.path.join('native', 'out', arch, 'magisk') for arch in archs + arch64)
    execv(args)

and as the platform was not Windows, it went into the else clause and check the existence of the binary "native/out/elf-cleaner". The binary elf_cleaner did not exist, so it tried to compile directly using g++ without passing a "-std=c++11" flag to the compiler.

All required environment variables were set correctly as specified in README.MD of this project, and the FrankeNDK were used to build. After I modified the code and passed a "-std=c++11" flag to the compiler, the compilation succeeded.

osm0sis commented 4 years ago

Ah, sounds good then. Likely dependent on distro g++ version, but the additional flag should be okay for all I'd think.

Make a PR with the change and link it to this issue please.

zivmc commented 4 years ago

Ah, sounds good then. Likely dependent on distro g++ version, but the additional flag should be okay for all I'd think.

Make a PR with the change and link it to this issue please.

Hi there, I have made a pull request, but I am not able to link the pull request to this issue.

Here is the link to the pull request: pr:2637

1tuanhasan commented 4 years ago

Bug