为什么有时候 LD_LIBRARY_PATH 不起作用(Why LD_LIBRARY_PATH is bad)

MEG预处理-Brainstorm Brainstorm下载和安装 去Brainstorm官网进行下载:打开网页后,点击左侧Download;在跳出来的界面中进行简单的注册,注册完成后可以进行下载 解压后得到“brainstorm3”的文件夹,里面包含Brainstorm工具包的所有文件,把这个文件夹放在某个路径如"E:\matlab_tools\brainstorm3"即可 同样在某个路径建立空的文件夹命名为:brainstorm_db用于工具包运行过程中自动存储生成的文件,所以这个文件中生成的数据不要手动移动删除 在Matlab中,把. 阅读详情

By David Barr.

Background

This is one system administrator's point of view why LD_LIBRARY_PATH, as frequently used, is bad. This is written from a SunOS 4.x/5.x (and to some extent Linux) point of view, but this also applies to most other UNIXes.

What LD_LIBRARY_PATH does

LD_LIBRARY_PATH is an environment variable you set to give the run-time shared library loader (ld.so) an extra set of directories to look for when searching for shared libraries. Multiple directories can be listed, separated with a colon (:). This list is prepended to the existing list of compiled-in loader paths for a given executable, and any system default loader paths.

For security reasons, LD_LIBRARY_PATH is ignored at runtime for executables that have their setuid or setgid bit set. This severely limits the usefulness of LD_LIBRARY_PATH.

Why was it invented?

There were a couple good reasons why it was invented:

  • To test out new library routines against an already compiled binary (for either backward compatibility or for new feature testing).
  • To have a short term way out in case you wanted to move a set of shared libraries to another location.

As an often unwanted side effect, LD_LIBRARY_PATH will also be searched at link (ld) stage after directories specified with -L (also if no -L flag is given).

Some good examples of how LD_LIBRARY_PATH is used:

  • When upgrading shared libraries, you can test out a library before replacing it.
  • In a similar vein, in case your upgrade program depends on shared libraries and may freak out if you replace a shared library out from under it, you can use LD_LIBRARY_PATH to point to a directory with copy of a shared libraries and then you can replace the system copy without worry. You can even undo things should things fail by moving the copy back.
  • X11 uses LD_LIBRARY_PATH during its build process. X11 distributes its fonts in “bdf” format, and during the build process it needs to “compile” the bdf files into “pcf” files. LD_LIBRARY_PATH is used to point the the build lib directory so it can run bdftopcf during the build stage before the shared libraries are installed.
  • Perl can be installed with most of its core code as a shared library. This is handy if you embed Perl in other programs -- you can compile them so they use the shared library and so you'll save memory at run time. However Perl uses Perl scripts at various points in the build and install process. The 'perl' binary won't run until its shared libraries are installed, unless LD_LIBRARY_PATH is used to bootstrap the process.

How has it been corrupted?

Too often people use it as a crutch for not doing the right thing (i.e. relying on the compiled in path). Often programs (even commercial ones) are compiled without any run-time loader paths at all, forcing you to have LD_LIBRARY_PATH set or else the program won't run.

LD_LIBRARY_PATH is one of those insidious things that once it gets set globally for a user, things tend to happen which cause people to rely on it being set. Eventually when LD_LIBRARY_PATH needs to be changed or removed, mass breakage will occur!

How does the shared loader work?

SunOS 4.x uses major and minor revision numbers. If you have a library Xt, then it's named something like libXt.so.4.10 (Major version 4, minor 10). If you update the library (to correct a bug, for example), you would install libX11.so.4.11 and applications would automatically use the new version. To do this, the loader must do a readdir() for every directory in the loader path and glob out the correct file name. This is quite expensive especially if the directories are large, contain symlinks, and/or are located over NFS.

Linux, SunOS 5.x and most other SYSV variants use only major revision numbers. A library Xt is just named something like libXt.so.4. (Linux confuses things by generally using major/minor library file names, but always include a symlink that is the actual library path referenced. So, for example, a library “libXt.so.6” is actually a symlink to “libXt.so.6.0”. The linker/loader actually looks for “libXt.so.6”.)

The loader works essentially the same except that you don't have minor library updates (you update the existing library) and the loader just does a stat() for each directory in the loader path. (This is much faster)

The bad old days before separate run-time vs link-time paths

Nowadays you specify the run-time path for an executable at link stage with the -R (or sometimes -rpath) flag to ld. There's also LD_RUN_PATH which is an environment variable which acts to ld just like specifying -R.

Before all this you had only -L, which applied not only during compile-time, but during run time as well. There was no way to say “use this directory during compile time” but “use this other directory at run time”. There were some rather spectacular failure modes that one could get in to because of this. For example, say you are building X11R6 in an NFS automounted directory /home/snoopy/src. X11R6 is made up of shared libraries as well as programs. The programs are compiled against the libraries when they are located in the build tree, not in their final installed location. Since the linker must resolve symbols at link time, you need a -L path that includes the link-time path in addition to the final run-time path of, say, /usr/local/X11R6/lib. Now all the programs which use shared libraries will look first in /home/snoopy/src for their libraries and then in the correct place. Now every time an X11R6 app starts up it NFS automounts its build directory! You probably removed the temporary build directory ages ago, but the linker will still search there. What's worse, say snoopy is down or no longer exists, no X11R6 apps will run! Bummer! Happily this all has been fixed, assuming your OS has a modern linker/loader. It also is worked around by specifying the final run time path first, before the build path in the -L options.

Evil Case Study #1

My first experience with this breakage was under SunOS 4.x, with OpenWindows. For some dumb reason, a few Sun OpenWindows apps were not compiled with correct run-time loader paths, forcing you to have LD_LIBRARY_PATH set all the time. Remember, at this time, in the global OpenWindows startup scripts the system would automatically set your LD_LIBRARY_PATH to be $OPENWINHOME/lib.

Okay, how did it break? Well, it just so happens that this site also had compiled X11R4 from source, in /usr/local/X11R4 . Things got really confusing because if you ever wanted to run the X11R4 apps, they would run against the OpenWindows libraries in /usr/openwin/lib, not the libraries in /usr/local/X11R4/lib! Things got even more confusing once X11R5 and then X11R6 came out. Now we had four different and often incompatible versions of a given shared library.

Hm. What do you do? If you set LD_LIBRARY_PATH to put OpenWindows first, then at best it will slow things down (since most people were running X11R5 and X11R6 stuff, searching for libraries in /usr/openwin/lib was a waste). At worst it caused spurious warnings (“ld.so: warning: libX11.x.y has older revision than expected z”) or caused apps to break altogether due to incompatibilities. It was also confusing to lots of people trying to compile X apps and forget to use -L.

What did I do? I whipped out emacs and binary edited the few OpenWindows apps which didn't have a correct run-time path compiled in, and changed to the correct location in /usr/openwin/lib. (it should be noted that these tended to be apps which were fixed with system patches.. alas it seems guys who build the patched versions didn't have the same environment as the FCS guys). I then changed all the startup scripts and removed any “setenv LD_LIBRARY_PATH” statements. I even put in an “unsetenv LD_LIBRARY_PATH” in my own .cshrc for good measure.

Evil Case Study #2

(based on a true story).

Due to licensing issues, it's common for commercial apps to ship in binary form a copy of the shared Motif library. Motif is a commercial product, and not all OS's come with it. It's a common toolkit for commercial programs to write applications against. It's also an evolving product, with ongoing bugfixes and new features.

Say application WidgetMan is one such application. In its startup script, it sets LD_LIBRARY_PATH to point to its copy of Motif so it uses that one when it runs. As it happens, WidgetMan is designed to launch other programs too. Unfortunately, when WidgetMan launches other apps, they inherit the LD_LIBRARY_PATH setting and some Motif based apps now break when run from WidgetMan because WidgetMan's Motif is incompatible with (but the same library version as) the system Motif library. Bummer!

Imagine if you had followed what some clueless commercial install apps tell you to do and set LD_LIBRARY_PATH globally!

Half-hearted attempts to improve things

Some OS's (e.g. Linux) have a configurable loader. You can configure what run-time paths to look in by modifying /etc/ld.so.conf. This is almost as bad a LD_LIBRARY_PATH! Install scripts should never modify this file! This file should contain only the standard library locations as shipped with the OS.

Canonical rules for handling LD_LIBRARY_PATH

  1. Never ever set LD_LIBRARY_PATH globally.
  2. If you must ship binaries that use shared libraries and want to allow your clients to install the program outside a 'standard' location, do one of the following:
    • Ship your binaries as .o files, and as part of the install process relink them with the correct installation library path.
    • Ship executables with a very long “dummy” run-time library path, and as part of the install process use a binary editor to substitute the correct install library path in the executable.
  3. If you are forced to set LD_LIBRARY_PATH, do so only as part of a wrapper.

Some software packages make you install a symlink from the standard location pointing to the real location. While this 'works', it does not solve the problem. What if you need to have two versions installed? Not to mention the fact that many vendors seem to choose stupid locations as their 'standard' location (like putting them in '/' or '/usr'). This also typically makes things difficult for network installations, since even though you install an application on a network directory, you need to go around to every computer on the network and make a symlink.

Thoughts on improving LD_LIBRARY_PATH implementations in UNIX

  • Remove the link-time aspect of LD_LIBRARY_PATH. (Solaris's ld will do this with the -i flag). Too often people just lazily set LD_LIBRARY_PATH so they don't have to specify -L, causing bad consequences at run time for other apps. Or on the flip side people will set LD_LIBRARY_PATH to fix some brokenness at run time with some app, but it will lead to confusion or breakage at compile time for some other app if they don't specify a correct -L path. It would be much cleaner if LD_LIBRARY_PATH only had influence at run-time. If necessary, invent some other environment variable for the job (LD_LINK_PATH ?).
  • Have OS's ship with programs which allow one to safely change an executable's run-time linker path.
  • Implement -s option to ldd which prints this run-time path for a given executable. (You can also see this with 'dump -Lv' in Solaris.)
  • Solaris 7 has a neat idea. There you can can specify a run time path which is also evaluated at run time. You link with an rpath of $ORIGIN/../lib. Here, $ORIGIN evaluates at run time to be the installation path of the binary. Now you can move the installation tree to another location entirely and everything will still work. We need this in other OS's! Unfortunately, at least in Solaris 7, $ORIGIN is considered a “relative” path (you can subvert it if you have a writable directory on the same filesystem because UNIX lets you hard link even a setuid executable) so it is ignored on setuid/setgid binaries. Sun has fixed this in Solaris 8. You can specify with crle(1) paths that are “trustworthy”.

 

INCA刷写App文件必看:A2l文件配置与ProF文件安装详解(2023最新版) 本文深入解析了使用INCA进行ECU应用程序刷写时,A2l文件配置与ProF文件安装的关键细节与避坑指南。重点阐述了A2l文件必须与待刷写二进制文件严格匹配的核心逻辑,以及ProF文件与Bootloader版本强相关的安装策略,帮助开发者解决刷写过程中的常见失败问题,确保刷写流程的稳定与可靠。 阅读详情

相关推荐

别再折腾老教程了!树莓派4B/5最新系统(Raspberry Pi OS Bookworm)红外遥控配置,用gpio-ir驱动一次成功

本文详细介绍了树莓派4B/5在最新Raspberry Pi OS Bookworm系统下配置红外遥控的完整指南,摒弃过时的lirc-rpi驱动,采用现代gpio-ir驱动方案。从硬件连接到软件配置,提供10分钟快速上手指南,涵盖驱动加载、信号测试、按键映射及高级应用场景,帮助用户高效完成红外遥控配置。

weixin_42520909的博客 442

设置LD_LIBRARY_PATH不起作用解决

遇到的现象: 将/usr/local/lib加入共享库搜索目录中: sudo vim /etc/profile 将export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib 加入到最后,保存该文件即可 重启linux 在终端执行 echo $ LD_LIBRARY_PATH ,之后执行结果中没有 /usr/local/lib 解决方法: 部分Linux系统设置LD_LIBRARY_PATH变量,并不能生效,此时需要将变...

danshiming的博客 1万+

Argoverse轨迹预测实战:从数据加载到地图API应用全解析

本文详细解析了Argoverse数据集在轨迹预测中的应用,从数据加载到地图API的实战技巧全涵盖。通过具体代码示例展示如何高效处理轨迹数据、实现可视化及基础预测模型,帮助开发者快速掌握自动驾驶轨迹预测的核心技术。特别介绍了Argoverse地图API的深度应用和性能优化经验。

weixin_29182481的博客 241

/etc/profile中配置LD_LIBRARY_PATH不生效问题分析

LD_LIBRARY_PATH配置不生效LIBRARY_PATH与LD_LIBRARY_PATH问题复现分析1:是否执行`source /etc/profile`分析2:`LD_LIBRARY_PATH`中配置的目录是否正确分析3:`LD_LIBRARY_PATH`中配置的目录,里面的动态库是否创建了软连接,若共享库没有创建软连接,也会找不到。 LIBRARY_PATH与LD_LIBRARY_PA...

elimuzi的博客 1万+

LabVIEW Modbus通讯实战:从安装库到读写寄存器的完整避坑指南

本文详细介绍了LabVIEW与Modbus通讯的完整实战指南,从安装库文件到读写寄存器的全流程操作,帮助开发者避开常见问题。通过虚拟串口配置、主机配置实战和高级调试技巧,提升工业自动化领域的通讯效率与可靠性。

milk8的博客 738

EBS开发_创建AP付款

--以下代码于20181002在Tony环境测试通过 /* BEGIN   --mo_global.init('SQLAP');   apps.fnd_global.apps_initialize(user_id => 1110, resp_id => 50597, resp_appl_id => 20003);   --mo_global.set_policy_context(...

蜗牛先生快跑的博客 4766

设置LD_LIBRARY_PATH不起作用(失效)

部分Linux系统设置LD_LIBRARY_PATH变量,并不能生效,此时需要将变量值写入/etc/ld.so.conf文件中,然后执行如下命令即可生效。 sudo ldconfig /etc/ld.so.conf

u013992330的博客 1万+

SAP FICO实操:如何修改已发生业务数据的成本中心公司代码(附RKACOR06程序运行指南)

本文详细解析了SAP FICO系统中使用RKACOR06程序修改已发生业务数据的成本中心和公司代码的实操方法。通过实战案例分享,提供从前期准备到后期验证的全流程指南,帮助企业应对组织架构重组或财务核算体系调整时的数据迁移挑战,特别适用于跨公司代码的成本中心划转场景。

weixin_27303545的博客 189

linux set LD_LIBRARY_PATH 不起作用的解决

LD_LIBRARY_PATH不起作用解决

richer_live的博客 1191

C# 的 Dapper 库高级进阶:刨根问底的深度解析

通过今天的探索,我们了解了 Dapper 的一些高级功能,包括如何处理复杂的查询、执行批量操作以及如何优化性能。希望这篇深入浅出的文章能帮助你在实际项目中更好地运用 Dapper!🎈好了,小伙伴们,今天的分享就到这里啦!如果你有任何疑问或者想要分享自己的经验,请在评论区留言。我们下次再见!👋上述示例代码仅供参考,实际使用时请根据项目需求进行适当调整。✨Dapper 官方文档Dapper 性能优化指南Dapper 动态参数使用来同时执行多个查询。利用Execute和Query方法来简化 SQL 调用。

java专栏 812

大模型角色提示词写得再严密,也能被绕过?我用攻击者思维把自己吓了一跳!

很多人以为提示词是“只要写清楚了,模型就能理解”,但实际上——模型并不具备严格的规则意识,它只是在预测下一句话。所以我们必须从“攻防”角度出发,设计更强的防护机制。我这次就是亲身踩坑,用红队思维模拟攻击,才意识到原来提示词注入这么容易中招。你如果也在做 AI 应用、角色系统、提示词驱动的功能,建议你也用类似方式测试一下,不然被绕了都不知道。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 1026

LD_LIBRARY_PATH 无效

问题描述:   修改了bin生成日志路径,日志不显示        更新最新的动态库,日志路径变更无效。                  用ldd命令查看bin的动态库,发现指向的不是自己设置的LD_LIBRARY_PATH                于是怀疑make 是制定了rpath选项               用readelf  -d  bi

lahm_z的博客 7794

SAP BOM反查(可追溯值至顶级)

功能介绍:BOM层数在7层以内的(含7层)的物料,执行后都可以查询到顶级的BOM,逻辑很简单,后期如BOM层数增加,可自行增加查询逻辑。 *&---------------------------------------------------------------------* *& Report ZBOM_CHECK *&---------------------...

SAP阿狸 6059

Linux环境变量LD_LIBRARY_PATH

Linux环境变量LD_LIBRARY_PATH

小原的博客 1万+

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

关于LD_LIBRARY_PATH的问题; 为什么说LD_LIBRARY_PATH不好

本人在/etc/environment中设置了环境变量: JAVA_HOME=/opt/java ORACLE_HOME=/home/user2/ LD_LIBRARY_PATH=/usr/lib:/usr/local/lib       正常情况下执行没有问题,但使用sudo XXXXXXX的时候,其他变量都好使,除了LD_LIBRARY_PATH;   经探索,找到原因:

nancygreen的专栏 3186

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Linux环境变量LD_LIBRARY_PATH和LIBRARY_PATH

LIBRARY_PATH和LD_LIBRARY_PATH是Linux下的两个环境变量,二者的含义和作用分别如下: LIBRARY_PATH环境变量用于在程序编译期间查找动态链接库时指定查找共享库的路径,例如,指定gcc编译需要用到的动态链接库的目录。设置方法如下(其中,LIBDIR1和LIBDIR2为两个库目录)

二爷的博客 7711

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

Why LD_LIBRARY_PATH is bad

Why LD_LIBRARY_PATH is bad by David Barr.

heyabo 828

Linux 共享库: LD_LIBRARY_PATH 与ld.so.conf

<br />Linux 运行的时候,是如何管理共享库(*.so)的?在 Linux 下面,共享库的寻找和加载是由 /lib/ld.so 实现的。 ld.so 在标准路经(/lib, /usr/lib) 中寻找应用程序用到的共享库。但是,如果需要用到的共享库在非标准路经,ld.so 怎么找到它呢?目前,Linux 通用的做法是将非标准路经加入 /etc/ld.so.conf,然后运行 ldconfig 生成 /etc/ld.so.cache。 ld.so 加载共享库的时候,会从 ld.so.cache 查找。

大道相通 956

linux环境变量LD_LIBRARY_PATH

LIBRARY_PATH和LD_LIBRARY_PATH是Linux下的两个环境变量,二者的含义和作用分别如下: LIBRARY_PATH环境变量用于在程序编译期间查找动态链接库时指定查找共享库的路径,例如,指定gcc编译需要用到的动态链接库的目录。设置方法如下(其中,LIBDIR1和LIBDIR2为两个库目录): export LIBRARY_PATH=LIBDIR1:LIBD...

diaoqu4574的博客 402

Linux 共享库:尽量避免 LD_LIBRARY_PATH

Linux 运行的时候,是如何管理共享库(*.so)的?在 Linux 下面,共享库的寻找和加载是由 /lib/ld.so 实现的。 ld.so 在标准路经(/lib, /usr/lib) 中寻找应用程序用到的共享库。但是,如果需要用到的共享库在非标准路经,ld.so 怎么找到它呢?目前,Linux 通用的做法是将非标准路经加入 /etc/ld.so.conf,然后运行 ldconfig 生成 /

2420

linux java.library.path,linux LD_LIBRARY_PATH环境变量

linux共享库位置配置(LD_LIBRARY_PATH环境变量 或者 更改/etc/ld.so.conf 或者使用-R选项)今天下午尝试使用libosip2,安装比较简单,按照自带的help文档里面的操作进行即可。$>mkdir linux-build$>cd linux-build$>../libosip2-2.2.0/configure$>make (最后2步要在管...

weixin_39630106的博客 1188

ldconfig详解 Linux 共享库: LD_LIBRARY_PATH 与ld.so.conf (新增库文件)

转自:http://blog.sina.com.cn/s/blog_698f90230100xedt.html Linux 运行的时候,是如何管理共享库(*.so)的?在 Linux 下面,共享库的寻找和加载是由/lib/ld.so 实现的。 ld.so 在标准路经(/lib, /usr/lib) 中寻找应用程序用到的共享库。 但是,如果需要用到的共享库在非标准路经,ld.so 怎么找到它呢? 目前,Linux 通用的做法是将非标准路经加入 /etc/ld.so.conf,然后运行 ldconfig 生.

chenxijie1985的专栏 2311

有关LD_LIBRARY_PATH与ld.so.conf

我之前写过一篇关于LD_LIBRARY_PATH与gcc/g++ -L的关系的文章,于是我自己用CPACK制作了一个Debian安装包,然后我在/home/.bashrc里添加了export LD_LIBRARY_PATH=/usr/loca/lib:$LD_LIBRARY_PATH,再然后老大告诉我这个方法不行,打回重新想办法,经过一番寻找终于找到了---ld.so.conf可以完美解...

bacanpi5506的博客 251

关于LD_LIBRARY_PATH

不知道是自己的记性不好还是根本没有用心,总是记不住“LD_LIBRARY_PATH”这个环境变量的名字。刚才在网上搜索了一下“LD_”,找到了关于这个环境变量的一些文章,其中有一篇文章《尽量避免LD_LIBRARY_PATH》,没看出来为什么要尽量避免,但是文中给出了几个链接,等有时间研究一下。下面是对全文的引用:Linux 运行的时候,是如何管理哪些共享库(*.so)的?在 Linux 下面,共

fibbery学习笔记 1418

[备份]Why LD_LIBRARY_PATH is bad

Why LD_LIBRARY_PATH is bad By David Barr, barr@visi.comBackground This is one system administrator's point of view why LD_LIBRARY_PATH, as frequently used, is bad. This is written from a SunOS 4....

zjumty 240

Linux 共享库:LD_LIBRARY_PATH 与ld.so.conf

转自:http://blog.sina.com.cn/s/blog_5623cddb0100e4sq.html LD_LIBRARY_PATH     Linux环境变量名,该环境变量主要用于指定查找共享库(动态链接库)时除了默认路径之外的其他路径。(该路径在默认路径之前查找)   移植程序时的经常碰到需要使用一些特定的动态库,而这些编译好的动态库放在我们自己建立的目录里,这时可以将这些目录...

xcw_1987的博客 369
上一篇: 一个ActiveX 对VC 运行库的依赖以及相关部署问题
下一篇: STC 51 单片机 浮点运算。
空指针1996
博客等级 码龄19年 17粉丝 28原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值