ubuntu环境下因pie选项导致双击启动失败的问题

发布时间 2023-12-31 16:30:16作者: jackieathome

在ubuntu环境下,链接可执行文件时增加-pie选项,双击可执行程序,无法正常启动。

对于这个现象,stackoverflow有个帖子,gcc creates mime type application/x-sharedlib instead of application/x-application,介绍的比较清晰,如下是相关的摘抄。

gcc doesn't set the mime type. mimetype guesses the appropriate mime type based on the contents of the file. For ELF files (most compiled binaries and shared libraries), the header contains a field e_type which identifies its type. If it is ET_DYN, then mimetype will treat it as a shared library.

By default, gcc/ld will produce binaries which set e_type to ET_EXEC, which get detected as application/x-executable. When the command-line option -pie is used, a position-independent executable is created, which may, like shared libraries, be loaded at different addresses and still work. Because this works so much like a shared library, to avoid too many changes to the loader, such binaries get marked as ET_DYN, even though they can be executed directly.

Some Linux distributions, yours included, have set -pie as the default. It's still possible to override this with -no-pie, but the fact that the mime type is misdetected should not be seen as a bug, and unless you know what you're doing, you shouldn't override it. -pie allows for some extra security protections that are fundamentally incompatible with -no-pie.

处理方法有如下:

方法一

链接可执行程序时,去掉-pie选项,增加-no-pie选项。
本方法降低了程序的安全性,不推荐使用。

方法二

参考恢复文件管理器中鼠标双击运行ELF可执行文件的功能,修改mime的配置,执行如下步骤:

  • 修改文件/usr/share/mime/packages/freedesktop.org.xml
  • 更新mime数据库,执行命令sudo update-mime-database /usr/share/mime

假如用户具备丰富的Linux和ubuntu使用经验,可以使用本方法。
但考虑到ubuntu版本比较多,本方法的兼容性可能没有那么好,毕竟不同版本ubuntu的配置文件,具体内容可能存在出入,指导用户修改配置的文档没有那么好写。

方法三

修改用户的操作习惯,指导用户通过快捷方式来启动程序。
创建快捷方式的方法,可参照官方指导UnityLaunchersAndDesktopFiles
操作步骤如下:

  • 创建app.desktop文件,样例内容如下
    [Desktop Entry]
    Version=1.0 # version of application
    Name=application name
    Comment=Comment for the application
    Exec=/path/to/application/startup.sh
    Icon=/path/to/application/icon.png
    Terminal=false
    Type=Application
    Categories=Utility;Application;
    
  • app.desktop增加可执行权限,执行如下命令:
    chmod +x app.desktop
    
  • app.desktop放置到应用目录下,执行如下命令:
    cp app.desktop /usr/share/applications/
    cp app.desktop ~/.local/share/applications/
    
  • app.desktop放到用户桌面。
    cp app.desktop ~/Desktop
    

本方法对于用户而言基本没有什么难度,新增的开发工作量其实并不大。
另行,使用脚本来启动程序,一些简单的操作比如环境检测、环境初始化类的操作,可以迁移到脚本中实现,简单、好用。

另外对于清除RPATH后导致应用程序无法直接启动的问题,可以通过本方法规避,即在脚本中修改LD_LIBRARY_PATH,将程序依赖库的路径写入到LD_LIBRARY_PATH中。