美文网首页
UPX /src/stub/Makefile 编译 armv7

UPX /src/stub/Makefile 编译 armv7

作者: that_is_this | 来源:发表于2018-03-11 19:59 被阅读309次
    1. 编译不同平台对应的 GCC 不同,这个是编译为 arm32,对应的 gcc 是 arm-linux-androideabi-gcc,这个 GCC 的安装命令: sudo apt-get install gcc-arm-linux-androideabi

    -march 对应的是系统架构,在跟踪 UPX 源码的时候,armv7 的 so 文件对应这个

    arm.v5t-linux.shlib%.h : tc_list = arm.v5t-linux.elf default
    arm.v5t-linux.shlib%.h : tc_bfdname = elf32-littlearm
    
    tc.arm.v5t-linux.elf.gcc  = arm-linux-androideabi-gcc -march=armv5t
    
    arm.v5t-linux.shlib%.h : $(srcdir)/src/$$T.S
        $(call tc,gcc) -c -x assembler-with-cpp $< -o tmp/$T.bin
        $(call tc,f-embed_objinfo,tmp/$T.bin)
        $(call tc,bin2h-c) tmp/$T.bin $@
    

    在 /src/stub/ 目录下:make 进行编译

    1. 下面是把原 Makefile 文件的 multiarch 改为 arm-linux-androideabi:
    # default multiarch-binutils
    tc.default.m-ar       = arm-linux-androideabi-ar
    tc.default.m-ld       = arm-linux-androideabi-ld $(if $(tc_bfdname),-b $(tc_bfdname))
    tc.default.m-nm       = arm-linux-androideabi-nm $(if $(tc_bfdname),--target=$(tc_bfdname))
    tc.default.m-objcopy  = arm-linux-androideabi-objcopy $(if $(tc_bfdname),-F $(tc_bfdname))
    tc.default.m-objdump  = arm-linux-androideabi-objdump $(if $(tc_bfdname),-b $(tc_bfdname)) $(if $(tc_bfdarch),-m $(tc_bfdarch))
    tc.default.m-readelf  = arm-linux-androideabi-readelf
    
    # default binutils
    tc.default.ld         = $(call tc,m-ld)
    tc.default.nm         = $(call tc,m-nm)
    tc.default.objcopy    = $(call tc,m-objcopy)
    tc.default.objdump    = $(call tc,m-objdump)
    tc.default.readelf    = $(call tc,m-readelf)
    
    
    1. 删除了其他平台架构的条目,只留以下的一个
    # /***********************************************************************
    # // arm.v5t-linux.shlib (arm.v5t)
    # ************************************************************************/
    
    arm.v5t-linux.shlib%.h : tc_list = arm.v5t-linux.elf default
    arm.v5t-linux.shlib%.h : tc_bfdname = elf32-littlearm
    
    tc.arm.v5t-linux.elf.gcc  = arm-linux-androideabi-gcc -march=armv5t
    
    arm.v5t-linux.shlib%.h : $(srcdir)/src/$$T.S
        $(call tc,gcc) -c -x assembler-with-cpp $< -o tmp/$T.bin
        $(call tc,f-embed_objinfo,tmp/$T.bin)
        $(call tc,bin2h-c) tmp/$T.bin $@
    
    1. arm64 GCC 安装

    gcc host.c -o host_32 -m32 //生成 32 位的 elf x86 文件...默认是 64 位的
    gcc -v // 查看版本信息

    arm-linux-androideabi-gcc host.c -o host_arm //生成 arm 汇编的 elf 文件

    sudo apt-get install gcc-aarch64-linux-gnu // 安装 arm64 交叉编译环境

    1. 附 arm64 Makefile 文件
    #
    # UPX stub Makefile - needs GNU make 3.81 or better
    #
    # You also will need a number of special build tools like various
    # cross-assemblers and cross-compilers - please see README.SRC
    # for details.
    #
    # Copyright (C) 1996-2017 Markus Franz Xaver Johannes Oberhumer
    #
    
    MAKEFLAGS += -rR
    .SUFFIXES:
    .SECONDEXPANSION:
    .NOTPARALLEL:
    export LC_ALL = C
    export SHELL = /bin/sh
    
    # internal make variables
    comma := ,
    empty :=
    space := $(empty) $(empty)
    tab   := $(empty)   $(empty)
    
    ifneq ($(findstring $(firstword $(MAKE_VERSION)),3.77 3.78 3.78.1 3.79 3.79.1 3.80),)
    $(error GNU make 3.81 or better is required)
    endif
    
    ifndef srcdir
    srcdir := $(dir $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)))
    srcdir := $(shell echo '$(srcdir)' | sed 's,/*$$,,')
    endif
    ifndef top_srcdir
    top_srcdir := $(srcdir)/../..
    endif
    include $(wildcard $(top_srcdir)/Makevars.global ./Makevars.local)
    
    # update $PATH for our special stub build tools
    ifneq ($(wildcard $(HOME)/local/bin/bin-upx/upx-stubtools-check-version),)
    export PATH := $(HOME)/local/bin/bin-upx:$(PATH)
    else ifneq ($(wildcard $(HOME)/.local/bin/bin-upx/upx-stubtools-check-version),)
    export PATH := $(HOME)/.local/bin/bin-upx:$(PATH)
    else ifneq ($(wildcard $(HOME)/bin/bin-upx/upx-stubtools-check-version),)
    export PATH := $(HOME)/bin/bin-upx:$(PATH)
    endif
    
    
    # /***********************************************************************
    # //
    # ************************************************************************/
    
    $(step 1)
    ifndef STUBS
    STUBS += arm64-linux.shlib-init.h
    endif
    
    
    ifndef default.targets
    ifeq ($(wildcard .all-stamp),)
    default.targets =
    default:
        @echo "UPX info: type 'make all' if you have all the required build tools."
    else
    default.targets = all
    default: $$(default.targets)
    endif
    endif
    
    
    all.targets ?= .upx-stubtools-stamp tmp/.tmp-stamp .all-stamp
    all: $$(all.targets)
    .upx-stubtools-stamp: $(MAKEFILE_LIST)
        upx-stubtools-check-version 20160918
        $(step 4)
        @echo "timestamp" > $@
    .DELETE_ON_ERROR: .upx-stubtools-stamp
    %/.tmp-stamp:
        @mkdir -p $(dir $@)
        @echo "timestamp" > $@
    .PRECIOUS: %/.tmp-stamp
    .all-stamp: $$(STUBS)
        @echo "timestamp" > $@
    
    ifeq ($(wildcard .all-stamp),)
    mostlyclean clean: distclean
    else
    mostlyclean clean: maintainer-clean
    endif
    distclean:
        rm -f .*-stamp
        rm -rf tmp
    maintainer-clean:
        rm -f .upx-stubtools-stamp
        rm -rf tmp
        rm -f $(STUBS)
    list-stubs: $(STUBS)
    #   @ls -l $(STUBS)
        @wc $(STUBS)
    
    .PHONY: default all mostlyclean clean distclean maintainer-clean list-stubs
    
    
    # util var for use in the rules - basename of the current target
    override T = $(basename $(notdir $@))
    
    # clear some vars, just in case
    LABEL_PREFIX =
    PP_FLAGS =
    tc_bfdarch =
    tc_bfdname =
    tc_list =
    tc_objdump_disasm_options =
    
    # commands
    ECHO_e     = /bin/echo -e
    ECHO_E     = /bin/echo -E
    PERL       = perl
    PYTHON     = python
    UNIX2DOS  := $(PERL) -i -pe 's/$$/\r/;'
    
    # trim (strip) trailing whitespace
    RTRIM     := sed -e 's/[ $(tab)]*$$//'
    # squeeze duplicate blank lines, delete empty first & last line
    BLSQUEEZE := $(RTRIM) | cat --squeeze-blank | sed -e '1{/^$$/d}' -e '$${/^$$/d}'
    # delete blank lines
    BLDEL     := $(RTRIM) | sed -e '/^$$/d'
    # delete blank lines at top (beginning) of file
    BLDELTOP  := sed -e '/./,$$!d'
    # delete blank lines at bottom (end) of file
    BLDELBOT  := sed -e ':a' -e '/^\n*$$/{$$d;N;ba' -e '}'
    
    
    # /***********************************************************************
    # // setup toolchain globals
    # ************************************************************************/
    
    # enumerate the names of all variables that will get tested (from basename and $(tc_list))
    __tc_varlist   = tc.$(basename $(notdir $@)).$1 $(foreach v,$(tc_list),tc.$v.$1)
    # return the name of the first variable that is not empty
    __tc_varsearch = $(firstword $(foreach v,$1,$(if $($v),$v,)))
    # error sentinel for missing commands
    __tc_FALSE     = false tc_FALSE: '1:$1 2:$2 3:$3 4:$4 5:$5' '$@' '$<' '$(tc_list)'
    # call (expand) the first variable that is not empty
      # [move the next comment line within the "tc" macro for debugging]
      # tc debug '1:$1 2:$2 3:$3 4:$4 5:$5' '$@' '$<' '$(tc_list)'
    define tc
      $(call $(call __tc_varsearch,$(call __tc_varlist,$1) __tc_FALSE),$2,$3,$4,$5)
    endef
    
    # default tools
    tc.default.bin2h      = $(PYTHON) $(top_srcdir)/src/stub/scripts/bin2h.py --ident=auto-stub
    ##tc.default.bin2h-c    = $(call tc,bin2h) --compress=14,15,0
    tc.default.bin2h-c    = $(call tc,bin2h) --compress=0
    tc.default.brandelf   = $(PYTHON) $(top_srcdir)/src/stub/scripts/brandelf.py $(if $(tc_bfdname),--bfdname=$(tc_bfdname))
    tc.default.gpp_inc    = $(PYTHON) $(top_srcdir)/src/stub/scripts/gpp_inc.py
    tc.default.gpp_mkdep  = $(PYTHON) $(top_srcdir)/src/stub/scripts/gpp_inc.py -o /dev/null
    
    tc.default.sstrip     = sstrip-20060518
    tc.default.xstrip     = $(PYTHON) $(top_srcdir)/src/stub/scripts/xstrip.py
    
    # default multiarch-binutils
    tc.default.m-ar       = aarch64-linux-android-ar
    tc.default.m-ld       = aarch64-linux-android-ld $(if $(tc_bfdname),-b $(tc_bfdname))
    tc.default.m-nm       = aarch64-linux-android-nm $(if $(tc_bfdname),--target=$(tc_bfdname))
    tc.default.m-objcopy  = aarch64-linux-android-objcopy $(if $(tc_bfdname),-F $(tc_bfdname))
    tc.default.m-objdump  = aarch64-linux-android-objdump $(if $(tc_bfdname),-b $(tc_bfdname)) $(if $(tc_bfdarch),-m $(tc_bfdarch))
    tc.default.m-readelf  = aarch64-linux-android-readelf
    
    # default binutils
    tc.default.ld         = $(call tc,m-ld)
    tc.default.nm         = $(call tc,m-nm)
    tc.default.objcopy    = $(call tc,m-objcopy)
    tc.default.objdump    = $(call tc,m-objdump)
    tc.default.readelf    = $(call tc,m-readelf)
    
    # default binutils functions
    define tc.default.f-embed_objinfo
        chmod a-x $1
        $(call tc,objcopy) --strip-unneeded $1
        $(call tc,objcopy) -R .text -R .data -R .bss $1
        $(call tc,objcopy) -R .comment -R .note -R .note.GNU-stack -R .reginfo $1
        $(call tc,objdump) -Dr $(tc_objdump_disasm_options) $1 | $(RTRIM) > $1.disasm
        $(call tc,objdump) -htr -w $1 | $(BLSQUEEZE) | sed -e '1s/^.*: *file format/file format/' > $1.dump
        $(call tc,xstrip) --with-dump=$1.dump $1
        cat $1.dump >> $1
    endef
    
    tc.default.f-objstrip-disasm.bin = @true
    tc.default.f-objstrip-disasm.o   = $(call tc,objdump) -dr $(tc_objdump_disasm_options) $1 | $(RTRIM) > $1.disasm
    tc.default.f-objstrip-disasm.obj = $(call tc,objdump) -dr $(tc_objdump_disasm_options) $1 | $(RTRIM) > $1.disasm
    define tc.default.f-objstrip
        chmod a-x $1
        $(call tc,objcopy) -R .comment -R .note -R .note.GNU-stack -R .reginfo $1
        $(call tc,f-objstrip-disasm$(suffix $1),$1)
    endef
    
    # some common arch settings
    tc.arch-i086.gcc      = i386-linux-gcc-3.4.6 -m32 -march=i386 -nostdinc -MMD -MT $@
    tc.arch-i086.wdis     = $(WATCOM)/binl/wdis
    tc.arch-i386.gcc      = i386-linux-gcc-3.4.6 -m32 -march=i386 -nostdinc -MMD -MT $@
    tc.arch-i386.djasm    = djasm-20060614
    
    
    
    
    # /***********************************************************************
    # // arm64-linux.shlib arm64        >> arm64-linux-objcopy-2.25     arm64-linux-objdump-2.25
    # ************************************************************************/
    arm64-linux.shlib%.h : tc_list = arm64-linux.elf default
    arm64-linux.shlib%.h : tc_bfdname = elf64-littleaarch64
    tc.arm64-linux.shlib-init.objcopy  = aarch64-linux-gnu-objcopy -F elf64-littleaarch64
    tc.arm64-linux.shlib-init.objdump  = aarch64-linux-gnu-objdump
    
    tc.arm64-linux.elf.gcc  = aarch64-linux-android-gcc
    
    arm64-linux.shlib%.h : $(srcdir)/src/$$T.S
        $(call tc,gcc) -c -x assembler-with-cpp $< -o tmp/$T.bin
        $(call tc,f-embed_objinfo,tmp/$T.bin)
        $(call tc,bin2h-c) tmp/$T.bin $@
    
    
    # /***********************************************************************
    # // dependencies
    # ************************************************************************/
    
    ifneq ($(strip $(STUBS)),)
    $(step 4)
    # FIXME: we want a dependency-only prerequisite here
    $(STUBS): tmp/.tmp-stamp $(MAKEFILE_LIST)
    $(STUBS): $(top_srcdir)/src/stub/scripts/bin2h.py
    $(STUBS): $(top_srcdir)/src/stub/scripts/xstrip.py
    endif
    -include tmp/*.d
    
    ifneq ($(strip $(STUBS)),)
    .DELETE_ON_ERROR: $(STUBS)
    endif
    
    
    # /***********************************************************************
    # // extra-all
    # // extra-clean
    # ************************************************************************/
    
    ifndef EXTRA_MAKEFILES
    EXTRA_MAKEFILES :=
    ##ifneq ($(filter extra-%,$(MAKECMDGOALS)),)
    EXTRA_MAKEFILES += $(sort $(wildcard src/arch/*/Makefile.extra))
    EXTRA_MAKEFILES += $(sort $(wildcard src/arch/*/*/Makefile.extra))
    EXTRA_MAKEFILES += $(sort $(wildcard src/arch/*/*/*/Makefile.extra))
    ####EXTRA_MAKEFILES += $(shell find src/arch -name Makefile.extra 2>/dev/null)
    ##endif
    endif
    
    ifneq ($(strip $(EXTRA_MAKEFILES)),)
    EXTRA_MAKEFILES.targets :=
    EXTRA_MAKEFILES.targets += $(addsuffix .~all,$(sort $(EXTRA_MAKEFILES)))
    EXTRA_MAKEFILES.targets += $(addsuffix .~clean,$(sort $(EXTRA_MAKEFILES)))
    
    $(EXTRA_MAKEFILES.targets): MAKEFLAGS += --no-print-directory
    $(EXTRA_MAKEFILES.targets): $$(basename $$@)
        $(MAKE) -C $(dir $@) -f $(basename $(notdir $@)) $(subst .~,,$(suffix $@))
    
    extra-all:   $$(filter %.~all,$$(EXTRA_MAKEFILES.targets))
    extra-clean: $$(filter %.~clean,$$(EXTRA_MAKEFILES.targets))
    
    .PHONY: extra-all extra-clean $(EXTRA_MAKEFILES.targets)
    endif
    
    # vim:set ts=8 sw=8 noet:
    
    

    相关文章

      网友评论

          本文标题:UPX /src/stub/Makefile 编译 armv7

          本文链接:https://www.haomeiwen.com/subject/zgywfftx.html