mirror of
https://gitee.com/openharmony/third_party_libbpf
synced 2024-12-04 00:52:46 +00:00
add elfio interface
Signed-off-by: Chenshi <chenshi51@huawei.com>
This commit is contained in:
parent
2cd2d03f63
commit
6a8736f44b
100
BUILD.gn
Normal file
100
BUILD.gn
Normal file
@ -0,0 +1,100 @@
|
||||
# Copyright (C) 2021 Huawei Device Co., Ltd.
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import("//build/ohos.gni")
|
||||
import("//build/ohos/ndk/ndk.gni")
|
||||
config("libbpf_config") {
|
||||
cflags = [
|
||||
"-Wno-incompatible-pointer-types",
|
||||
"-Wimplicit-function-declaration",
|
||||
"-Wno-tautological-constant-out-of-range-compare",
|
||||
"-Wno-constant-conversion",
|
||||
"-Wno-unknown-attributes",
|
||||
"-Wno-bitwise-op-parentheses",
|
||||
"-Wno-shift-op-parentheses",
|
||||
"-Wno-sign-compare",
|
||||
"-Wno-unused-function",
|
||||
"-fno-omit-frame-pointer",
|
||||
"-mno-omit-leaf-frame-pointer",
|
||||
"-fno-inline",
|
||||
"-fno-optimize-sibling-calls",
|
||||
"-ferror-limit=0",
|
||||
"-Wno-unused-variable",
|
||||
"-Wno-uninitialized",
|
||||
]
|
||||
defines = [
|
||||
"HAVE_ELFIO",
|
||||
]
|
||||
}
|
||||
|
||||
config("libbpf_public_config") {
|
||||
include_dirs = [
|
||||
"./src",
|
||||
"./include",
|
||||
"./include/uapi",
|
||||
"//third_party/zlib",
|
||||
"//third_party/elfio/c_wrapper",
|
||||
"//third_party/elfio/elfio",
|
||||
]
|
||||
}
|
||||
|
||||
ohos_shared_library("libbpf") {
|
||||
deps = [
|
||||
"//third_party/elfio:elfio",
|
||||
"//third_party/zlib:libz",
|
||||
]
|
||||
sources = [
|
||||
"./src/bpf.c",
|
||||
"./src/bpf_core_read.h",
|
||||
"./src/bpf_endian.h",
|
||||
"./src/bpf_gen_internal.h",
|
||||
"./src/bpf.h",
|
||||
"./src/bpf_helper_defs.h",
|
||||
"./src/bpf_helpers.h",
|
||||
"./src/bpf_prog_linfo.c",
|
||||
"./src/bpf_tracing.h",
|
||||
"./src/btf.c",
|
||||
"./src/btf_dump.c",
|
||||
"./src/btf.h",
|
||||
"./src/gen_loader.c",
|
||||
"./src/hashmap.c",
|
||||
"./src/hashmap.h",
|
||||
"./src/libbpf.c",
|
||||
"./src/libbpf_common.h",
|
||||
"./src/libbpf_errno.c",
|
||||
"./src/libbpf.h",
|
||||
"./src/libbpf_internal.h",
|
||||
"./src/libbpf_legacy.h",
|
||||
"./src/libbpf_probes.c",
|
||||
"./src/libbpf_version.h",
|
||||
"./src/netlink.c",
|
||||
"./src/nlattr.c",
|
||||
"./src/nlattr.h",
|
||||
"./src/relo_core.c",
|
||||
"./src/relo_core.h",
|
||||
"./src/ringbuf.c",
|
||||
"./src/skel_internal.h",
|
||||
"./src/str_error.c",
|
||||
"./src/str_error.h",
|
||||
"./src/strset.c",
|
||||
"./src/strset.h",
|
||||
"./src/xsk.c",
|
||||
"./src/xsk.h",
|
||||
]
|
||||
configs = [ ":libbpf_config" ]
|
||||
public_configs = [ ":libbpf_public_config" ]
|
||||
subsystem_name = "developtools"
|
||||
part_name = "profiler"
|
||||
install_enable = true
|
||||
}
|
||||
|
541
NOTICE
Normal file
541
NOTICE
Normal file
@ -0,0 +1,541 @@
|
||||
Version 0.7
|
||||
==============================================================================
|
||||
The libbpf is under LGPL-2.1 OR BSD-2-Clause:
|
||||
==============================================================================
|
||||
Valid-License-Identifier: LGPL-2.1
|
||||
Valid-License-Identifier: LGPL-2.1+
|
||||
SPDX-URL: https://spdx.org/licenses/LGPL-2.1.html
|
||||
Usage-Guide:
|
||||
To use this license in source code, put one of the following SPDX
|
||||
tag/value pairs into a comment according to the placement
|
||||
guidelines in the licensing rules documentation.
|
||||
For 'GNU Lesser General Public License (LGPL) version 2.1 only' use:
|
||||
SPDX-License-Identifier: LGPL-2.1
|
||||
For 'GNU Lesser General Public License (LGPL) version 2.1 or any later
|
||||
version' use:
|
||||
SPDX-License-Identifier: LGPL-2.1+
|
||||
License-Text:
|
||||
|
||||
GNU LESSER GENERAL PUBLIC LICENSE
|
||||
Version 2.1, February 1999
|
||||
|
||||
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
|
||||
Everyone is permitted to copy and distribute verbatim copies of this
|
||||
license document, but changing it is not allowed.
|
||||
|
||||
[This is the first released version of the Lesser GPL. It also counts as
|
||||
the successor of the GNU Library Public License, version 2, hence the
|
||||
version number 2.1.]
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your freedom to
|
||||
share and change it. By contrast, the GNU General Public Licenses are
|
||||
intended to guarantee your freedom to share and change free software--to
|
||||
make sure the software is free for all its users.
|
||||
|
||||
This license, the Lesser General Public License, applies to some specially
|
||||
designated software packages--typically libraries--of the Free Software
|
||||
Foundation and other authors who decide to use it. You can use it too, but
|
||||
we suggest you first think carefully about whether this license or the
|
||||
ordinary General Public License is the better strategy to use in any
|
||||
particular case, based on the explanations below.
|
||||
|
||||
When we speak of free software, we are referring to freedom of use, not
|
||||
price. Our General Public Licenses are designed to make sure that you have
|
||||
the freedom to distribute copies of free software (and charge for this
|
||||
service if you wish); that you receive source code or can get it if you
|
||||
want it; that you can change the software and use pieces of it in new free
|
||||
programs; and that you are informed that you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
distributors to deny you these rights or to ask you to surrender these
|
||||
rights. These restrictions translate to certain responsibilities for you if
|
||||
you distribute copies of the library or if you modify it.
|
||||
|
||||
For example, if you distribute copies of the library, whether gratis or for
|
||||
a fee, you must give the recipients all the rights that we gave you. You
|
||||
must make sure that they, too, receive or can get the source code. If you
|
||||
link other code with the library, you must provide complete object files to
|
||||
the recipients, so that they can relink them with the library after making
|
||||
changes to the library and recompiling it. And you must show them these
|
||||
terms so they know their rights.
|
||||
|
||||
We protect your rights with a two-step method: (1) we copyright the
|
||||
library, and (2) we offer you this license, which gives you legal
|
||||
permission to copy, distribute and/or modify the library.
|
||||
|
||||
To protect each distributor, we want to make it very clear that there is no
|
||||
warranty for the free library. Also, if the library is modified by someone
|
||||
else and passed on, the recipients should know that what they have is not
|
||||
the original version, so that the original author's reputation will not be
|
||||
affected by problems that might be introduced by others.
|
||||
|
||||
Finally, software patents pose a constant threat to the existence of any
|
||||
free program. We wish to make sure that a company cannot effectively
|
||||
restrict the users of a free program by obtaining a restrictive license
|
||||
from a patent holder. Therefore, we insist that any patent license obtained
|
||||
for a version of the library must be consistent with the full freedom of
|
||||
use specified in this license.
|
||||
|
||||
Most GNU software, including some libraries, is covered by the ordinary GNU
|
||||
General Public License. This license, the GNU Lesser General Public
|
||||
License, applies to certain designated libraries, and is quite different
|
||||
from the ordinary General Public License. We use this license for certain
|
||||
libraries in order to permit linking those libraries into non-free
|
||||
programs.
|
||||
|
||||
When a program is linked with a library, whether statically or using a
|
||||
shared library, the combination of the two is legally speaking a combined
|
||||
work, a derivative of the original library. The ordinary General Public
|
||||
License therefore permits such linking only if the entire combination fits
|
||||
its criteria of freedom. The Lesser General Public License permits more lax
|
||||
criteria for linking other code with the library.
|
||||
|
||||
We call this license the "Lesser" General Public License because it does
|
||||
Less to protect the user's freedom than the ordinary General Public
|
||||
License. It also provides other free software developers Less of an
|
||||
advantage over competing non-free programs. These disadvantages are the
|
||||
reason we use the ordinary General Public License for many
|
||||
libraries. However, the Lesser license provides advantages in certain
|
||||
special circumstances.
|
||||
|
||||
For example, on rare occasions, there may be a special need to encourage
|
||||
the widest possible use of a certain library, so that it becomes a de-facto
|
||||
standard. To achieve this, non-free programs must be allowed to use the
|
||||
library. A more frequent case is that a free library does the same job as
|
||||
widely used non-free libraries. In this case, there is little to gain by
|
||||
limiting the free library to free software only, so we use the Lesser
|
||||
General Public License.
|
||||
|
||||
In other cases, permission to use a particular library in non-free programs
|
||||
enables a greater number of people to use a large body of free
|
||||
software. For example, permission to use the GNU C Library in non-free
|
||||
programs enables many more people to use the whole GNU operating system, as
|
||||
well as its variant, the GNU/Linux operating system.
|
||||
|
||||
Although the Lesser General Public License is Less protective of the users'
|
||||
freedom, it does ensure that the user of a program that is linked with the
|
||||
Library has the freedom and the wherewithal to run that program using a
|
||||
modified version of the Library.
|
||||
|
||||
The precise terms and conditions for copying, distribution and modification
|
||||
follow. Pay close attention to the difference between a "work based on the
|
||||
library" and a "work that uses the library". The former contains code
|
||||
derived from the library, whereas the latter must be combined with the
|
||||
library in order to run.
|
||||
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License Agreement applies to any software library or other program
|
||||
which contains a notice placed by the copyright holder or other
|
||||
authorized party saying it may be distributed under the terms of this
|
||||
Lesser General Public License (also called "this License"). Each
|
||||
licensee is addressed as "you".
|
||||
|
||||
A "library" means a collection of software functions and/or data
|
||||
prepared so as to be conveniently linked with application programs
|
||||
(which use some of those functions and data) to form executables.
|
||||
|
||||
The "Library", below, refers to any such software library or work which
|
||||
has been distributed under these terms. A "work based on the Library"
|
||||
means either the Library or any derivative work under copyright law:
|
||||
that is to say, a work containing the Library or a portion of it, either
|
||||
verbatim or with modifications and/or translated straightforwardly into
|
||||
another language. (Hereinafter, translation is included without
|
||||
limitation in the term "modification".)
|
||||
|
||||
"Source code" for a work means the preferred form of the work for making
|
||||
modifications to it. For a library, complete source code means all the
|
||||
source code for all modules it contains, plus any associated interface
|
||||
definition files, plus the scripts used to control compilation and
|
||||
installation of the library.
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of running
|
||||
a program using the Library is not restricted, and output from such a
|
||||
program is covered only if its contents constitute a work based on the
|
||||
Library (independent of the use of the Library in a tool for writing
|
||||
it). Whether that is true depends on what the Library does and what the
|
||||
program that uses the Library does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Library's complete
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the notices
|
||||
that refer to this License and to the absence of any warranty; and
|
||||
distribute a copy of this License along with the Library.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Library or any portion of it,
|
||||
thus forming a work based on the Library, and copy and distribute such
|
||||
modifications or work under the terms of Section 1 above, provided that
|
||||
you also meet all of these conditions:
|
||||
|
||||
a) The modified work must itself be a software library.
|
||||
|
||||
b) You must cause the files modified to carry prominent notices stating
|
||||
that you changed the files and the date of any change.
|
||||
|
||||
c) You must cause the whole of the work to be licensed at no charge to
|
||||
all third parties under the terms of this License.
|
||||
|
||||
d) If a facility in the modified Library refers to a function or a table
|
||||
of data to be supplied by an application program that uses the
|
||||
facility, other than as an argument passed when the facility is
|
||||
invoked, then you must make a good faith effort to ensure that, in
|
||||
the event an application does not supply such function or table, the
|
||||
facility still operates, and performs whatever part of its purpose
|
||||
remains meaningful.
|
||||
|
||||
(For example, a function in a library to compute square roots has a
|
||||
purpose that is entirely well-defined independent of the
|
||||
application. Therefore, Subsection 2d requires that any
|
||||
application-supplied function or table used by this function must be
|
||||
optional: if the application does not supply it, the square root
|
||||
function must still compute square roots.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Library, and
|
||||
can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based on
|
||||
the Library, the distribution of the whole must be on the terms of this
|
||||
License, whose permissions for other licensees extend to the entire
|
||||
whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Library.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Library
|
||||
with the Library (or with a work based on the Library) on a volume of a
|
||||
storage or distribution medium does not bring the other work under the
|
||||
scope of this License.
|
||||
|
||||
3. You may opt to apply the terms of the ordinary GNU General Public
|
||||
License instead of this License to a given copy of the Library. To do
|
||||
this, you must alter all the notices that refer to this License, so that
|
||||
they refer to the ordinary GNU General Public License, version 2,
|
||||
instead of to this License. (If a newer version than version 2 of the
|
||||
ordinary GNU General Public License has appeared, then you can specify
|
||||
that version instead if you wish.) Do not make any other change in these
|
||||
notices.
|
||||
|
||||
Once this change is made in a given copy, it is irreversible for that
|
||||
copy, so the ordinary GNU General Public License applies to all
|
||||
subsequent copies and derivative works made from that copy.
|
||||
|
||||
This option is useful when you wish to copy part of the code of the
|
||||
Library into a program that is not a library.
|
||||
|
||||
4. You may copy and distribute the Library (or a portion or derivative of
|
||||
it, under Section 2) in object code or executable form under the terms
|
||||
of Sections 1 and 2 above provided that you accompany it with the
|
||||
complete corresponding machine-readable source code, which must be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange.
|
||||
|
||||
If distribution of object code is made by offering access to copy from a
|
||||
designated place, then offering equivalent access to copy the source
|
||||
code from the same place satisfies the requirement to distribute the
|
||||
source code, even though third parties are not compelled to copy the
|
||||
source along with the object code.
|
||||
|
||||
5. A program that contains no derivative of any portion of the Library, but
|
||||
is designed to work with the Library by being compiled or linked with
|
||||
it, is called a "work that uses the Library". Such a work, in isolation,
|
||||
is not a derivative work of the Library, and therefore falls outside the
|
||||
scope of this License.
|
||||
|
||||
However, linking a "work that uses the Library" with the Library creates
|
||||
an executable that is a derivative of the Library (because it contains
|
||||
portions of the Library), rather than a "work that uses the
|
||||
library". The executable is therefore covered by this License. Section 6
|
||||
states terms for distribution of such executables.
|
||||
|
||||
When a "work that uses the Library" uses material from a header file
|
||||
that is part of the Library, the object code for the work may be a
|
||||
derivative work of the Library even though the source code is
|
||||
not. Whether this is true is especially significant if the work can be
|
||||
linked without the Library, or if the work is itself a library. The
|
||||
threshold for this to be true is not precisely defined by law.
|
||||
|
||||
If such an object file uses only numerical parameters, data structure
|
||||
layouts and accessors, and small macros and small inline functions (ten
|
||||
lines or less in length), then the use of the object file is
|
||||
unrestricted, regardless of whether it is legally a derivative
|
||||
work. (Executables containing this object code plus portions of the
|
||||
Library will still fall under Section 6.)
|
||||
|
||||
Otherwise, if the work is a derivative of the Library, you may
|
||||
distribute the object code for the work under the terms of Section
|
||||
6. Any executables containing that work also fall under Section 6,
|
||||
whether or not they are linked directly with the Library itself.
|
||||
|
||||
6. As an exception to the Sections above, you may also combine or link a
|
||||
"work that uses the Library" with the Library to produce a work
|
||||
containing portions of the Library, and distribute that work under terms
|
||||
of your choice, provided that the terms permit modification of the work
|
||||
for the customer's own use and reverse engineering for debugging such
|
||||
modifications.
|
||||
|
||||
You must give prominent notice with each copy of the work that the
|
||||
Library is used in it and that the Library and its use are covered by
|
||||
this License. You must supply a copy of this License. If the work during
|
||||
execution displays copyright notices, you must include the copyright
|
||||
notice for the Library among them, as well as a reference directing the
|
||||
user to the copy of this License. Also, you must do one of these things:
|
||||
|
||||
a) Accompany the work with the complete corresponding machine-readable
|
||||
source code for the Library including whatever changes were used in
|
||||
the work (which must be distributed under Sections 1 and 2 above);
|
||||
and, if the work is an executable linked with the Library, with the
|
||||
complete machine-readable "work that uses the Library", as object
|
||||
code and/or source code, so that the user can modify the Library and
|
||||
then relink to produce a modified executable containing the modified
|
||||
Library. (It is understood that the user who changes the contents of
|
||||
definitions files in the Library will not necessarily be able to
|
||||
recompile the application to use the modified definitions.)
|
||||
|
||||
b) Use a suitable shared library mechanism for linking with the
|
||||
Library. A suitable mechanism is one that (1) uses at run time a copy
|
||||
of the library already present on the user's computer system, rather
|
||||
than copying library functions into the executable, and (2) will
|
||||
operate properly with a modified version of the library, if the user
|
||||
installs one, as long as the modified version is interface-compatible
|
||||
with the version that the work was made with.
|
||||
|
||||
c) Accompany the work with a written offer, valid for at least three
|
||||
years, to give the same user the materials specified in Subsection
|
||||
6a, above, for a charge no more than the cost of performing this
|
||||
distribution.
|
||||
|
||||
d) If distribution of the work is made by offering access to copy from a
|
||||
designated place, offer equivalent access to copy the above specified
|
||||
materials from the same place.
|
||||
|
||||
e) Verify that the user has already received a copy of these materials
|
||||
or that you have already sent this user a copy.
|
||||
|
||||
For an executable, the required form of the "work that uses the Library"
|
||||
must include any data and utility programs needed for reproducing the
|
||||
executable from it. However, as a special exception, the materials to be
|
||||
distributed need not include anything that is normally distributed (in
|
||||
either source or binary form) with the major components (compiler,
|
||||
kernel, and so on) of the operating system on which the executable runs,
|
||||
unless that component itself accompanies the executable.
|
||||
|
||||
It may happen that this requirement contradicts the license restrictions
|
||||
of other proprietary libraries that do not normally accompany the
|
||||
operating system. Such a contradiction means you cannot use both them
|
||||
and the Library together in an executable that you distribute.
|
||||
|
||||
7. You may place library facilities that are a work based on the Library
|
||||
side-by-side in a single library together with other library facilities
|
||||
not covered by this License, and distribute such a combined library,
|
||||
provided that the separate distribution of the work based on the Library
|
||||
and of the other library facilities is otherwise permitted, and provided
|
||||
that you do these two things:
|
||||
|
||||
a) Accompany the combined library with a copy of the same work based on
|
||||
the Library, uncombined with any other library facilities. This must
|
||||
be distributed under the terms of the Sections above.
|
||||
|
||||
b) Give prominent notice with the combined library of the fact that part
|
||||
of it is a work based on the Library, and explaining where to find
|
||||
the accompanying uncombined form of the same work.
|
||||
|
||||
8. You may not copy, modify, sublicense, link with, or distribute the
|
||||
Library except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense, link with, or distribute the
|
||||
Library is void, and will automatically terminate your rights under this
|
||||
License. However, parties who have received copies, or rights, from you
|
||||
under this License will not have their licenses terminated so long as
|
||||
such parties remain in full compliance.
|
||||
|
||||
9. You are not required to accept this License, since you have not signed
|
||||
it. However, nothing else grants you permission to modify or distribute
|
||||
the Library or its derivative works. These actions are prohibited by law
|
||||
if you do not accept this License. Therefore, by modifying or
|
||||
distributing the Library (or any work based on the Library), you
|
||||
indicate your acceptance of this License to do so, and all its terms and
|
||||
conditions for copying, distributing or modifying the Library or works
|
||||
based on it.
|
||||
|
||||
10. Each time you redistribute the Library (or any work based on the
|
||||
Library), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute, link with or modify the Library
|
||||
subject to these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted
|
||||
herein. You are not responsible for enforcing compliance by third
|
||||
parties with this License.
|
||||
|
||||
11. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Library at all. For example, if a patent license
|
||||
would not permit royalty-free redistribution of the Library by all
|
||||
those who receive copies directly or indirectly through you, then the
|
||||
only way you could satisfy both it and this License would be to refrain
|
||||
entirely from distribution of the Library.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply, and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system which is implemented
|
||||
by public license practices. Many people have made generous
|
||||
contributions to the wide range of software distributed through that
|
||||
system in reliance on consistent application of that system; it is up
|
||||
to the author/donor to decide if he or she is willing to distribute
|
||||
software through any other system and a licensee cannot impose that
|
||||
choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
12. If the distribution and/or use of the Library is restricted in certain
|
||||
countries either by patents or by copyrighted interfaces, the original
|
||||
copyright holder who places the Library under this License may add an
|
||||
explicit geographical distribution limitation excluding those
|
||||
countries, so that distribution is permitted only in or among countries
|
||||
not thus excluded. In such case, this License incorporates the
|
||||
limitation as if written in the body of this License.
|
||||
|
||||
13. The Free Software Foundation may publish revised and/or new versions of
|
||||
the Lesser General Public License from time to time. Such new versions
|
||||
will be similar in spirit to the present version, but may differ in
|
||||
detail to address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Library
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and
|
||||
conditions either of that version or of any later version published by
|
||||
the Free Software Foundation. If the Library does not specify a license
|
||||
version number, you may choose any version ever published by the Free
|
||||
Software Foundation.
|
||||
|
||||
14. If you wish to incorporate parts of the Library into other free
|
||||
programs whose distribution conditions are incompatible with these,
|
||||
write to the author to ask for permission. For software which is
|
||||
copyrighted by the Free Software Foundation, write to the Free Software
|
||||
Foundation; we sometimes make exceptions for this. Our decision will be
|
||||
guided by the two goals of preserving the free status of all
|
||||
derivatives of our free software and of promoting the sharing and reuse
|
||||
of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
|
||||
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
||||
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE LIBRARY IS WITH
|
||||
YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
|
||||
NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU FOR
|
||||
DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL
|
||||
DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE LIBRARY
|
||||
(INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED
|
||||
INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF
|
||||
THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR
|
||||
OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Libraries
|
||||
|
||||
If you develop a new library, and you want it to be of the greatest
|
||||
possible use to the public, we recommend making it free software that
|
||||
everyone can redistribute and change. You can do so by permitting
|
||||
redistribution under these terms (or, alternatively, under the terms of the
|
||||
ordinary General Public License).
|
||||
|
||||
To apply these terms, attach the following notices to the library. It is
|
||||
safest to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least the
|
||||
"copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
one line to give the library's name and an idea of what it does.
|
||||
Copyright (C) year name of author
|
||||
|
||||
This library is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation; either version 2.1 of the License, or (at
|
||||
your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful, but WITHOUT
|
||||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this library; if not, write to the Free Software Foundation,
|
||||
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Also add
|
||||
information on how to contact you by electronic and paper mail.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the library, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in
|
||||
the library `Frob' (a library for tweaking knobs) written
|
||||
by James Random Hacker.
|
||||
|
||||
signature of Ty Coon, 1 April 1990
|
||||
Ty Coon, President of Vice
|
||||
That's all there is to it!
|
||||
|
||||
Valid-License-Identifier: BSD-2-Clause
|
||||
|
||||
SPDX-URL: https://spdx.org/licenses/BSD-2-Clause.html
|
||||
Usage-Guide:
|
||||
To use the BSD 2-clause "Simplified" License put the following SPDX
|
||||
tag/value pair into a comment according to the placement guidelines in
|
||||
the licensing rules documentation:
|
||||
SPDX-License-Identifier: BSD-2-Clause
|
||||
License-Text:
|
||||
|
||||
Copyright (c) <year> <owner> . All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright
|
||||
notice, this list of conditions and the following disclaimer in the
|
||||
documentation and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGE.
|
11
README.OpenSource
Normal file
11
README.OpenSource
Normal file
@ -0,0 +1,11 @@
|
||||
[
|
||||
{
|
||||
"Name": "libbpf",
|
||||
"License": "LGPL-2.1 OR BSD-2-Clause",
|
||||
"License File": "LICENSE",
|
||||
"Version Number": "0.7",
|
||||
"Owner": "xiazhonglin@huawei.com",
|
||||
"Upstream URL": "https://github.com/libbpf/libbpf",
|
||||
"Description": "a third party library to use eBPF"
|
||||
}
|
||||
]
|
92
README_OH.md
Normal file
92
README_OH.md
Normal file
@ -0,0 +1,92 @@
|
||||
# libbpf
|
||||
|
||||
仓库包含第三方开源软件libbpf,libbpf是eBPF技术的一种实现,eBPF提供给开发者一种内核事件和用户进程事件发生时安全注入代码的机制,避免开发者直接进行内核编程时可能导致的系统锁定、内存损坏、进程崩溃和安全漏洞等问题。开发者基于libbpf第三方开源库高效便捷开发eBPF程序,应用于网络过滤、程序跟踪、性能分析以及调试等需求场景。
|
||||
|
||||
# 目录结构
|
||||
|
||||
```
|
||||
docs/ 文档
|
||||
fuzz/ fuzz测试
|
||||
include/ 头文件
|
||||
scripts/ 脚本
|
||||
src/ 源文件
|
||||
LICENSE 证书文件
|
||||
README.md 英文说明
|
||||
README_zh.md 中文说明
|
||||
```
|
||||
|
||||
# OpenHarmony如何集成libbpf
|
||||
|
||||
## 1.头文件引入
|
||||
|
||||
```
|
||||
#include "libbpf.h"
|
||||
#include "bpf.h"
|
||||
```
|
||||
|
||||
## 2.BUILD.gn添加引用
|
||||
|
||||
```
|
||||
deps += ["//third_party/libbpf:libbpf"]
|
||||
```
|
||||
## 3.示例代码
|
||||
```
|
||||
# kprobe.bpf.c BPF程序
|
||||
# 指定LICENSE
|
||||
char LICENSE[] SEC("license") = "Dual BSD/GPL";
|
||||
# 进入do_unlinkat函数时调用
|
||||
SEC("kprobe/do_unlinkat")
|
||||
int BPF_KPROBE(do_unlinkat, int dfd, struct filename *name)
|
||||
{
|
||||
pid_t pid;
|
||||
const char *filename;
|
||||
|
||||
pid = bpf_get_current_pid_tgid() >> 32;
|
||||
filename = BPF_CORE_READ(name, name);
|
||||
bpf_printk("KPROBE ENTRY pid = %d, filename = %s\n", pid, filename);
|
||||
return 0;
|
||||
}
|
||||
# 退出do_unlinkat函数时调用
|
||||
SEC("kretprobe/do_unlinkat")
|
||||
int BPF_KRETPROBE(do_unlinkat_exit, long ret)
|
||||
{
|
||||
pid_t pid;
|
||||
|
||||
pid = bpf_get_current_pid_tgid() >> 32;
|
||||
bpf_printk("KPROBE EXIT: pid = %d, ret = %ld\n", pid, ret);
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
```
|
||||
# 用bpftool生成BPF skeleton脚手架文件
|
||||
$ bpftool gen skeleton kprobe.bpf.o > kprobe.skel.h
|
||||
```
|
||||
```
|
||||
# kprobe.c 将BPF程序加载进内核并附着到内核指定位置
|
||||
#include kprobe.skel.h
|
||||
struct kprobe_bpf *skel;
|
||||
int err;
|
||||
libbpf_set_strict_mode(LIBBPF_STRICT_ALL);
|
||||
/* 加载并验证BPF程序 */
|
||||
skel = kprobe_bpf__open_and_load();
|
||||
if (!skel) {
|
||||
fprintf(stderr, "Failed to open BPF skeleton\n");
|
||||
return 1;
|
||||
}
|
||||
/* 将BPF程序附加到跟踪点 */
|
||||
err = kprobe_bpf__attach(skel);
|
||||
/* 销毁和回收资源 */
|
||||
kprobe_bpf__destroy(skel);
|
||||
```
|
||||
# libbpf相关知识文档
|
||||
|
||||
bpf参考指南[https://nakryiko.com/posts/bpf-core-reference-guide/](https://nakryiko.com/posts/bpf-core-reference-guide/)
|
||||
|
||||
libbpf开发指导[https://nakryiko.com/posts/bcc-to-libbpf-howto-guide/](https://nakryiko.com/posts/bcc-to-libbpf-howto-guide/)
|
||||
|
||||
libbpf开发示例[https://github.com/libbpf/libbpf-bootstrap](https://github.com/libbpf/libbpf-bootstrap)
|
||||
# License
|
||||
|
||||
`SPDX-License-Identifier: BSD-2-Clause OR LGPL-2.1`
|
||||
|
||||
|
153
README_zh.md
Normal file
153
README_zh.md
Normal file
@ -0,0 +1,153 @@
|
||||
这是[Linux源码bpf](https://kernel.googlesource.com/pub/scm/linux/kernel/git/bpf/bpf-next)的`tools/lib/bpf`目录及其支持的头文件的一个镜像。同步的所有细节都可以在`scripts/sync-kernel.sh`脚本中找到。
|
||||
|
||||
此仓中的一些头文件(`include/linux/*.h`)对应文件是[Linux源码bpf](https://kernel.googlesource.com/pub/scm/linux/kernel/git/bpf/bpf-next)的`tools/include/linux/*.h,使得libbpf能够编译成功。
|
||||
|
||||
# BPF/libbpf用法和问题
|
||||
|
||||
请查看[libbpf引导程序](https://github.com/libbpf/libbpf-bootstrap)和配套得[博客文章](https://nakryiko.com/posts/libbpf-bootstrap/),使用libbpf构建BPF应用程序的示例。
|
||||
|
||||
[libbpf-tools](https://github.com/iovisor/bcc/tree/master/libbpf-tools)也是基于libbpf的跟踪工具的一个很好的实现。
|
||||
|
||||
另请参见[“BPF CO-RE参考指南”](https://nakryiko.com/posts/bpf-core-reference-guide/),涵盖构建BPF CO-RE应用程序的实际方面,
|
||||
|
||||
以及[“BPF CO-RE”](https://nakryiko.com/posts/bpf-portability-and-co-re/)对于BPF可移植性问题和BPF CO-RE起源的一般介绍。
|
||||
|
||||
所有一般BPF问题,包括内核功能、libbpf API以及申请,应发送邮件至[bpf@vger.kernel.org](mailto:bpf@vger.kernel.org)。
|
||||
|
||||
你可以订阅[此处](http://vger.kernel.org/vger-lists.html#bpf)和搜索其档案[此处](https://lore.kernel.org/bpf/). 在提出新问题之前请搜索档案,很可能这已经是在之前被定位或回答。
|
||||
|
||||
[bpf@vger.kernel.org](mailto:bpf@vger.kernel.org)被很多人管理,他们会很乐意尝试帮助你解决任何问题。
|
||||
|
||||
# 构建指导
|
||||
|
||||
libelf是libbpf的内部依赖项,因此需要链接并且必须安装在系统上,应用程序才能工作。
|
||||
|
||||
pkg-config默认用于查找libelf,调用的程序可以是用“PKG_CONFIG”重写。
|
||||
|
||||
如果不希望在构建时使用“pkg config”,可以通过调用make时设置`NO_PKG_CONFIG=1`。
|
||||
|
||||
构建静态库libbpf.a和动态库libbpf.so:
|
||||
|
||||
```
|
||||
$cd src
|
||||
$make
|
||||
```
|
||||
|
||||
仅生成静态库libbpf.a
|
||||
|
||||
```
|
||||
$cd src
|
||||
$mkdir build root
|
||||
$BUILD_STATIC_ONLY=y OBJDIR=build DESTDIR=root make install
|
||||
```
|
||||
|
||||
构建静态库libbpf.a和动态库libbpf.so
|
||||
|
||||
```
|
||||
$cd src
|
||||
$PKG_CONFIG_PATH=/build/root/lib64/pkgconfig DESTDIR=/build/root make install
|
||||
```
|
||||
|
||||
# 发行版
|
||||
|
||||
从此镜像打包libbpf的发行版:
|
||||
|
||||
-[Fedora](https://src.fedoraproject.org/rpms/libbpf)
|
||||
|
||||
-[Gentoo](https://packages.gentoo.org/packages/dev-libs/libbpf)
|
||||
|
||||
-[Debian](https://packages.debian.org/source/sid/libbpf)
|
||||
|
||||
-[Arch](https://www.archlinux.org/packages/extra/x86_64/libbpf/)
|
||||
|
||||
-[Ubuntu](https://packages.ubuntu.com/source/impish/libbpf)
|
||||
|
||||
-[Alpine](https://pkgs.alpinelinux.org/packages?name=libbpf)
|
||||
|
||||
从镜像打包优于从内核源打包的好处:
|
||||
|
||||
* 跨发行版的一致版本控制。
|
||||
|
||||
* 与任何特定内核无关,对旧内核的透明处理。
|
||||
|
||||
Libbpf被设计为内核不可知的,可以跨多个内核版本。
|
||||
|
||||
它有内置的机制,可以兼容老版本缺少某些功能的内核
|
||||
|
||||
因此,libbpf没有绑定到特定的内核版本,并且可以/应该独立地打包和版本化。
|
||||
|
||||
* 通过以下方式进行持续集成测试
|
||||
|
||||
[TravisCI](https://travis-ci.org/libbpf/libbpf).
|
||||
|
||||
* 通过[LGTM](https://lgtm.com/projects/g/libbpf/libbpf)和[Coverity](https://scan.coverity.com/projects/libbpf)进行静态代码分析
|
||||
|
||||
libbpf的包依赖性,包名称可能因发行版而异:
|
||||
|
||||
* zlib
|
||||
|
||||
* libelf
|
||||
|
||||
# BPF CO-RE(编译一次-到处运行)
|
||||
|
||||
libbpf支持构建支持BPF CO-RE的应用程序,这与[BCC](https://github.com/iovisor/bcc/)不同,不需要Clang/LLVM运行时部署到目标服务器并且不依赖于内核层的headers可用。
|
||||
但是它依赖于使用[BTF](https://www.kernel.org/doc/html/latest/bpf/btf.html)构建的内核
|
||||
|
||||
尽管如此,一些主要的Linux发行版已经内置了内核BTF:
|
||||
|
||||
* Fedora31+
|
||||
* RHEL 8.2+
|
||||
* OpenSUSE Tumbleweed(下一个版本,截至2020-06-04)
|
||||
* Arch Linux(来自内核5.7.1.arch1-1)
|
||||
* Manjaro(如果在2021 06月18日之后编译,则来自内核5.4)
|
||||
* Ubuntu 20.10
|
||||
* Debian 11(amd64/arm64)
|
||||
|
||||
如果您的内核没有内置BTF,则需要构建自定义内核
|
||||
|
||||
您需要:
|
||||
|
||||
* pahole 1.16+工具(dwarves包的一部分),用于执行DWARF到BTF转换;
|
||||
|
||||
* 使用`CONFIG_DEBUG_INFO_BTF=y`选项构建的内核;
|
||||
|
||||
* 您可以通过查找`/sys/kernel/btf/vmlinux`文件来检查内核是否内置了BTF:
|
||||
|
||||
```
|
||||
$ ls -la /sys/kernel/btf/vmlinux
|
||||
-r--r--r--. 1 root root 3541561 Jun 2 18:16 /sys/kernel/btf/vmlinux
|
||||
```
|
||||
|
||||
要开发和构建BPF程序,您需要Clang/LLVM 10+。以下发行版默认情况下具有Clang/LLVM 10+包:
|
||||
|
||||
* Fedora 32+
|
||||
|
||||
* Ubuntu 20.04+
|
||||
|
||||
* Arch Linux
|
||||
|
||||
* Ubuntu 20.10(LLVM 11)
|
||||
|
||||
* Debian 11(LLVM 11)
|
||||
|
||||
* Alpine 3.13+
|
||||
|
||||
否则,请确保在您的系统上更新它。
|
||||
|
||||
以下资源有助于理解什么是BPF CO-RE以及如何使用它:
|
||||
|
||||
* [BPF CO-RE参考指南](https://nakryiko.com/posts/bpf-core-reference-guide/)
|
||||
|
||||
* [BPF可移植性和CO-RE](https://nakryiko.com/posts/bpf-portability-and-co-re/)
|
||||
|
||||
* [HOWTO:BCC到libbpf的转换](https://nakryiko.com/posts/bcc-to-libbpf-howto-guide/)
|
||||
|
||||
* [BCC仓库中的libbpf工具](https://github.com/iovisor/bcc/tree/master/libbpf-tools)
|
||||
|
||||
# 许可证
|
||||
|
||||
本作品根据BSD 2条款许可证和GNU LGPL v2.1许可证双重许可。
|
||||
|
||||
如果您使用这项工作,您可以在其中一项中进行选择。
|
||||
|
||||
`SPDX-License-Identifier: BSD-2-Clause OR LGPL-2.1`
|
31
bundle.json
Normal file
31
bundle.json
Normal file
@ -0,0 +1,31 @@
|
||||
{
|
||||
"name": "@ohos/libbpf",
|
||||
"description": "Libbpf is a set of tools for building BPF applications",
|
||||
"version": "Release_0.7.0",
|
||||
"license": "LGPL-2.1 OR BSD-2-Clause License",
|
||||
"publishAs": "code-segment",
|
||||
"segment": {
|
||||
"destPath": "third_party/libbpf"
|
||||
},
|
||||
"dirs": {},
|
||||
"scripts": {},
|
||||
"licensePath": "LICENSE",
|
||||
"component": {
|
||||
"name": "libbpf",
|
||||
"subsystem": "thirdparty",
|
||||
"syscap": [],
|
||||
"features": [],
|
||||
"adapted_system_type": [],
|
||||
"rom": "",
|
||||
"ram": "",
|
||||
"deps": {
|
||||
"components": [],
|
||||
"third_party": []
|
||||
},
|
||||
"build": {
|
||||
"sub_component": [],
|
||||
"inner_kits": [],
|
||||
"test": []
|
||||
}
|
||||
}
|
||||
}
|
113
src/btf.c
113
src/btf.c
@ -15,7 +15,22 @@
|
||||
#include <linux/kernel.h>
|
||||
#include <linux/err.h>
|
||||
#include <linux/btf.h>
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
#include <gelf.h>
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_ELFIO
|
||||
#include "elfio_c_wrapper.h"
|
||||
|
||||
typedef struct Elf64_Ehdr Elf64_Ehdr;
|
||||
typedef struct Elf64_Shdr Elf64_Shdr;
|
||||
typedef struct {
|
||||
void *d_buf;
|
||||
size_t d_size;
|
||||
} Elf_Data;
|
||||
#endif
|
||||
|
||||
#include "btf.h"
|
||||
#include "bpf.h"
|
||||
#include "libbpf.h"
|
||||
@ -885,22 +900,50 @@ struct btf *btf__new(const void *data, __u32 size)
|
||||
return libbpf_ptr(btf_new(data, size, NULL));
|
||||
}
|
||||
|
||||
#ifdef HAVE_ELFIO
|
||||
static Elf64_Shdr *elf_sec_hdr_by_idx(pelfio_t elf, size_t idx, Elf64_Shdr *sheader)
|
||||
{
|
||||
psection_t psection = elfio_get_section_by_index(elf, idx);
|
||||
|
||||
sheader->sh_name = elfio_section_get_name_string_offset(psection);
|
||||
sheader->sh_type = elfio_section_get_type(psection);
|
||||
sheader->sh_flags = elfio_section_get_flags(psection);
|
||||
sheader->sh_addr = elfio_section_get_address(psection);
|
||||
sheader->sh_offset = elfio_section_get_offset(psection);
|
||||
sheader->sh_size = elfio_section_get_size(psection);
|
||||
sheader->sh_link = elfio_section_get_link(psection);
|
||||
sheader->sh_info = elfio_section_get_info(psection);
|
||||
sheader->sh_addralign = elfio_section_get_addr_align(psection);
|
||||
sheader->sh_entsize = elfio_section_get_entry_size(psection);
|
||||
|
||||
return sheader;
|
||||
}
|
||||
#endif
|
||||
|
||||
static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
|
||||
struct btf_ext **btf_ext)
|
||||
{
|
||||
Elf_Data *btf_data = NULL, *btf_ext_data = NULL;
|
||||
int err = 0, fd = -1, idx = 0;
|
||||
struct btf *btf = NULL;
|
||||
#ifdef HAVE_LIBELF
|
||||
Elf_Scn *scn = NULL;
|
||||
Elf *elf = NULL;
|
||||
GElf_Ehdr ehdr;
|
||||
#elif defined HAVE_ELFIO
|
||||
pelfio_t elf;
|
||||
Elf64_Ehdr ehdr;
|
||||
Elf_Data btf_data_temp, btf_ext_data_temp;
|
||||
#endif
|
||||
size_t shstrndx;
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
if (elf_version(EV_CURRENT) == EV_NONE) {
|
||||
pr_warn("failed to init libelf for %s\n", path);
|
||||
return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
|
||||
}
|
||||
|
||||
#endif
|
||||
fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
err = -errno;
|
||||
@ -910,45 +953,96 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
|
||||
|
||||
err = -LIBBPF_ERRNO__FORMAT;
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
elf = elf_begin(fd, ELF_C_READ, NULL);
|
||||
#elif defined HAVE_ELFIO
|
||||
elf = elfio_new();
|
||||
if (!elfio_load(elf, path)) {
|
||||
printf( "Can't load ELF file\n" );
|
||||
goto done;
|
||||
}
|
||||
#endif
|
||||
if (!elf) {
|
||||
pr_warn("failed to open %s as ELF file\n", path);
|
||||
goto done;
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
if (!gelf_getehdr(elf, &ehdr)) {
|
||||
#elif defined HAVE_ELFIO
|
||||
ssize_t nread = read(fd, &ehdr, sizeof(Elf64_Ehdr));
|
||||
if(nread != sizeof(Elf64_Ehdr)) {
|
||||
#endif
|
||||
pr_warn("failed to get EHDR from %s\n", path);
|
||||
goto done;
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
if (elf_getshdrstrndx(elf, &shstrndx)) {
|
||||
#elif defined HAVE_ELFIO
|
||||
shstrndx = elfio_get_section_name_str_index(elf);
|
||||
if(shstrndx < 0) {
|
||||
#endif
|
||||
pr_warn("failed to get section names section index for %s\n",
|
||||
path);
|
||||
goto done;
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
if (!elf_rawdata(elf_getscn(elf, shstrndx), NULL)) {
|
||||
pr_warn("failed to get e_shstrndx from %s\n", path);
|
||||
goto done;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
while ((scn = elf_nextscn(elf, scn)) != NULL) {
|
||||
GElf_Shdr sh;
|
||||
#elif defined HAVE_ELFIO
|
||||
psection_t psection = elfio_get_section_by_name(elf, ".shstrtab");
|
||||
if (!psection )
|
||||
goto done;
|
||||
|
||||
pstring_t pstring = elfio_string_section_accessor_new(psection);
|
||||
if (!pstring )
|
||||
goto done;
|
||||
|
||||
int secno = elfio_get_sections_num(elf);
|
||||
|
||||
for ( int i = 0; i < secno; i++ ) {
|
||||
Elf64_Shdr sh;
|
||||
#endif
|
||||
char *name;
|
||||
|
||||
idx++;
|
||||
#if defined HAVE_LIBELF
|
||||
if (gelf_getshdr(scn, &sh) != &sh) {
|
||||
#elif defined HAVE_ELFIO
|
||||
if (!elf_sec_hdr_by_idx(elf, i, &sh)) {
|
||||
#endif
|
||||
pr_warn("failed to get section(%d) header from %s\n",
|
||||
idx, path);
|
||||
goto done;
|
||||
}
|
||||
#if defined HAVE_LIBELF
|
||||
name = elf_strptr(elf, shstrndx, sh.sh_name);
|
||||
#elif defined HAVE_ELFIO
|
||||
name = elfio_string_get_string(pstring, sh.sh_name);
|
||||
#endif
|
||||
if (!name) {
|
||||
pr_warn("failed to get section(%d) name from %s\n",
|
||||
idx, path);
|
||||
goto done;
|
||||
}
|
||||
if (strcmp(name, BTF_ELF_SEC) == 0) {
|
||||
#if defined HAVE_LIBELF
|
||||
btf_data = elf_getdata(scn, 0);
|
||||
#elif defined HAVE_ELFIO
|
||||
psection_t psection_index = elfio_get_section_by_index(elf, i);
|
||||
btf_data_temp.d_buf = (void*)elfio_section_get_data(psection_index);
|
||||
btf_data_temp.d_size = elfio_section_get_size(psection_index);
|
||||
btf_data = &btf_data_temp;
|
||||
#endif
|
||||
if (!btf_data) {
|
||||
pr_warn("failed to get section(%d, %s) data from %s\n",
|
||||
idx, name, path);
|
||||
@ -956,7 +1050,14 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
|
||||
}
|
||||
continue;
|
||||
} else if (btf_ext && strcmp(name, BTF_EXT_ELF_SEC) == 0) {
|
||||
#if defined HAVE_LIBELF
|
||||
btf_ext_data = elf_getdata(scn, 0);
|
||||
#elif defined HAVE_ELFIO
|
||||
psection_t psection_index = elfio_get_section_by_index(elf, i);
|
||||
btf_ext_data_temp.d_buf = (void*)elfio_section_get_data(psection_index);
|
||||
btf_ext_data_temp.d_size = elfio_section_get_size(psection_index);
|
||||
btf_ext_data = &btf_ext_data_temp;
|
||||
#endif
|
||||
if (!btf_ext_data) {
|
||||
pr_warn("failed to get section(%d, %s) data from %s\n",
|
||||
idx, name, path);
|
||||
@ -977,7 +1078,11 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
|
||||
if (err)
|
||||
goto done;
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
switch (gelf_getclass(elf)) {
|
||||
#elif defined HAVE_ELFIO
|
||||
switch (elfio_get_class(elf)) {
|
||||
#endif
|
||||
case ELFCLASS32:
|
||||
btf__set_pointer_size(btf, 4);
|
||||
break;
|
||||
@ -999,7 +1104,13 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
|
||||
}
|
||||
done:
|
||||
if (elf)
|
||||
#ifdef HAVE_LIBELF
|
||||
elf_end(elf);
|
||||
#elif defined HAVE_ELFIO
|
||||
elfio_delete(elf);
|
||||
if(pstring)
|
||||
elfio_string_section_accessor_delete(pstring);
|
||||
#endif
|
||||
close(fd);
|
||||
|
||||
if (!err)
|
||||
@ -4683,7 +4794,9 @@ struct btf *btf__load_vmlinux_btf(void)
|
||||
if (locations[i].raw_btf)
|
||||
btf = btf__parse_raw(path);
|
||||
else
|
||||
#ifdef HAVE_LIBELF
|
||||
btf = btf__parse_elf(path, NULL);
|
||||
#endif
|
||||
err = libbpf_get_error(btf);
|
||||
pr_debug("loading kernel BTF '%s': %d\n", path, err);
|
||||
if (err)
|
||||
|
356
src/libbpf.c
356
src/libbpf.c
@ -44,8 +44,11 @@
|
||||
#include <sys/vfs.h>
|
||||
#include <sys/utsname.h>
|
||||
#include <sys/resource.h>
|
||||
#ifdef HAVE_LIBELF
|
||||
#include <libelf.h>
|
||||
#include <gelf.h>
|
||||
#endif
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
#include "libbpf.h"
|
||||
@ -56,6 +59,27 @@
|
||||
#include "hashmap.h"
|
||||
#include "bpf_gen_internal.h"
|
||||
|
||||
#ifdef HAVE_ELFIO
|
||||
#include "elfio_c_wrapper.h"
|
||||
#include <linux/memfd.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <limits.h>
|
||||
|
||||
typedef struct Elf64_Ehdr Elf64_Ehdr;
|
||||
typedef struct Elf64_Shdr Elf64_Shdr;
|
||||
typedef struct Elf64_Sym Elf64_Sym;
|
||||
typedef struct Elf64_Rel Elf64_Rel;
|
||||
typedef struct {
|
||||
void *d_buf;
|
||||
size_t d_size;
|
||||
} Elf_Data;
|
||||
|
||||
#define ELF64_ST_TYPE(val) ELF_ST_TYPE (val)
|
||||
#define ELF64_ST_BIND(val) ELF_ST_BIND (val)
|
||||
#define elf_errmsg(val) "error"
|
||||
|
||||
#endif
|
||||
|
||||
#ifndef BPF_FS_MAGIC
|
||||
#define BPF_FS_MAGIC 0xcafe4a11
|
||||
#endif
|
||||
@ -464,7 +488,12 @@ enum sec_type {
|
||||
|
||||
struct elf_sec_desc {
|
||||
enum sec_type sec_type;
|
||||
#if defined HAVE_LIBELF
|
||||
Elf64_Shdr *shdr;
|
||||
#elif defined HAVE_ELFIO
|
||||
psection_t psection;
|
||||
Elf_Data realdata;
|
||||
#endif
|
||||
Elf_Data *data;
|
||||
};
|
||||
|
||||
@ -472,7 +501,16 @@ struct elf_state {
|
||||
int fd;
|
||||
const void *obj_buf;
|
||||
size_t obj_buf_sz;
|
||||
#if defined HAVE_LIBELF
|
||||
Elf *elf;
|
||||
#elif defined HAVE_ELFIO
|
||||
pelfio_t elf;
|
||||
Elf64_Ehdr eheader;
|
||||
pstring_t shstring;
|
||||
pstring_t strstring;
|
||||
Elf_Data realsymbols;
|
||||
Elf_Data realst_ops_data;
|
||||
#endif
|
||||
Elf64_Ehdr *ehdr;
|
||||
Elf_Data *symbols;
|
||||
Elf_Data *st_ops_data;
|
||||
@ -555,11 +593,26 @@ struct bpf_object {
|
||||
|
||||
static const char *elf_sym_str(const struct bpf_object *obj, size_t off);
|
||||
static const char *elf_sec_str(const struct bpf_object *obj, size_t off);
|
||||
#ifdef HAVE_LIBELF
|
||||
static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx);
|
||||
static Elf_Scn *elf_sec_by_name(const struct bpf_object *obj, const char *name);
|
||||
#endif
|
||||
#if defined HAVE_LIBELF
|
||||
static Elf64_Shdr *elf_sec_hdr(const struct bpf_object *obj, Elf_Scn *scn);
|
||||
#elif defined HAVE_ELFIO
|
||||
static Elf64_Shdr *elf_sec_hdr_by_idx(const struct bpf_object *obj, size_t idx, Elf64_Shdr *sheader);
|
||||
#endif
|
||||
#if defined HAVE_LIBELF
|
||||
static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn);
|
||||
#elif defined HAVE_ELFIO
|
||||
static const char *elf_sec_name_by_idx(const struct bpf_object *obj, size_t idx);
|
||||
#endif
|
||||
#if defined HAVE_LIBELF
|
||||
static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn);
|
||||
#elif defined HAVE_ELFIO
|
||||
static Elf_Data *elf_sec_data_by_name(const struct bpf_object *obj, const char *name, Elf_Data *data);
|
||||
static Elf_Data *elf_sec_data_by_idx(const struct bpf_object *obj, size_t idx, Elf_Data *data);
|
||||
#endif
|
||||
static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx);
|
||||
static Elf64_Rel *elf_rel_by_idx(Elf_Data *data, size_t idx);
|
||||
|
||||
@ -1224,7 +1277,17 @@ static void bpf_object__elf_finish(struct bpf_object *obj)
|
||||
return;
|
||||
|
||||
if (obj->efile.elf) {
|
||||
#if defined HAVE_LIBELF
|
||||
elf_end(obj->efile.elf);
|
||||
#elif defined HAVE_ELFIO
|
||||
if (obj->efile.shstring) {
|
||||
elfio_string_section_accessor_delete(obj->efile.shstring);
|
||||
}
|
||||
if (obj->efile.strstring) {
|
||||
elfio_string_section_accessor_delete(obj->efile.strstring);
|
||||
}
|
||||
elfio_delete(obj->efile.elf);
|
||||
#endif
|
||||
obj->efile.elf = NULL;
|
||||
}
|
||||
obj->efile.symbols = NULL;
|
||||
@ -1241,7 +1304,11 @@ static int bpf_object__elf_init(struct bpf_object *obj)
|
||||
{
|
||||
Elf64_Ehdr *ehdr;
|
||||
int err = 0;
|
||||
#ifdef HAVE_LIBELF
|
||||
Elf *elf;
|
||||
#elif defined HAVE_ELFIO
|
||||
pelfio_t elf;
|
||||
#endif
|
||||
|
||||
if (obj->efile.elf) {
|
||||
pr_warn("elf: init internal error\n");
|
||||
@ -1253,7 +1320,17 @@ static int bpf_object__elf_init(struct bpf_object *obj)
|
||||
* obj_buf should have been validated by
|
||||
* bpf_object__open_buffer().
|
||||
*/
|
||||
#ifdef HAVE_LIBELF
|
||||
elf = elf_memory((char *)obj->efile.obj_buf, obj->efile.obj_buf_sz);
|
||||
#elif defined HAVE_ELFIO
|
||||
char memfd_path[PATH_MAX] = {0};
|
||||
elf = elfio_new();
|
||||
int fdm = syscall(__NR_memfd_create, "bpfelf", MFD_CLOEXEC);
|
||||
ftruncate(fdm, obj->efile.obj_buf_sz);
|
||||
write(fdm, (char *)obj->efile.obj_buf, obj->efile.obj_buf_sz);
|
||||
snprintf(memfd_path, PATH_MAX, "/proc/self/fd/%d", fdm);
|
||||
elfio_load(elf, memfd_path);
|
||||
#endif
|
||||
} else {
|
||||
obj->efile.fd = open(obj->path, O_RDONLY | O_CLOEXEC);
|
||||
if (obj->efile.fd < 0) {
|
||||
@ -1265,7 +1342,9 @@ static int bpf_object__elf_init(struct bpf_object *obj)
|
||||
return err;
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
elf = elf_begin(obj->efile.fd, ELF_C_READ_MMAP, NULL);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (!elf) {
|
||||
@ -1276,6 +1355,7 @@ static int bpf_object__elf_init(struct bpf_object *obj)
|
||||
|
||||
obj->efile.elf = elf;
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
if (elf_kind(elf) != ELF_K_ELF) {
|
||||
err = -LIBBPF_ERRNO__FORMAT;
|
||||
pr_warn("elf: '%s' is not a proper ELF object\n", obj->path);
|
||||
@ -1283,18 +1363,26 @@ static int bpf_object__elf_init(struct bpf_object *obj)
|
||||
}
|
||||
|
||||
if (gelf_getclass(elf) != ELFCLASS64) {
|
||||
#elif defined HAVE_ELFIO
|
||||
if (elfio_get_class(elf) != ELFCLASS64 ) {
|
||||
#endif
|
||||
err = -LIBBPF_ERRNO__FORMAT;
|
||||
pr_warn("elf: '%s' is not a 64-bit ELF object\n", obj->path);
|
||||
goto errout;
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
obj->efile.ehdr = ehdr = elf64_getehdr(elf);
|
||||
#elif defined HAVE_ELFIO
|
||||
obj->efile.ehdr = ehdr = (Elf64_Ehdr*)obj->efile.obj_buf;
|
||||
#endif
|
||||
if (!obj->efile.ehdr) {
|
||||
pr_warn("elf: failed to get ELF header from %s: %s\n", obj->path, elf_errmsg(-1));
|
||||
err = -LIBBPF_ERRNO__FORMAT;
|
||||
goto errout;
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
if (elf_getshdrstrndx(elf, &obj->efile.shstrndx)) {
|
||||
pr_warn("elf: failed to get section names section index for %s: %s\n",
|
||||
obj->path, elf_errmsg(-1));
|
||||
@ -1309,6 +1397,9 @@ static int bpf_object__elf_init(struct bpf_object *obj)
|
||||
err = -LIBBPF_ERRNO__FORMAT;
|
||||
goto errout;
|
||||
}
|
||||
#elif defined HAVE_ELFIO
|
||||
obj->efile.shstrndx = elfio_get_section_name_str_index(elf);
|
||||
#endif
|
||||
|
||||
/* Old LLVM set e_machine to EM_NONE */
|
||||
if (ehdr->e_type != ET_REL || (ehdr->e_machine && ehdr->e_machine != EM_BPF)) {
|
||||
@ -1376,14 +1467,22 @@ static int find_elf_sec_sz(const struct bpf_object *obj, const char *name, __u32
|
||||
{
|
||||
int ret = -ENOENT;
|
||||
Elf_Data *data;
|
||||
#ifdef HAVE_LIBELF
|
||||
Elf_Scn *scn;
|
||||
#endif
|
||||
|
||||
*size = 0;
|
||||
if (!name)
|
||||
return -EINVAL;
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
scn = elf_sec_by_name(obj, name);
|
||||
data = elf_sec_data(obj, scn);
|
||||
#elif defined HAVE_ELFIO
|
||||
Elf_Data realdata;
|
||||
data = &realdata;
|
||||
data = elf_sec_data_by_name(obj, name, data);
|
||||
#endif
|
||||
if (data) {
|
||||
ret = 0; /* found it */
|
||||
*size = data->d_size;
|
||||
@ -1592,7 +1691,11 @@ static int bpf_object__init_global_data_maps(struct bpf_object *obj)
|
||||
|
||||
switch (sec_desc->sec_type) {
|
||||
case SEC_DATA:
|
||||
#if defined HAVE_LIBELF
|
||||
sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
|
||||
#elif defined HAVE_ELFIO
|
||||
sec_name = elf_sec_name_by_idx(obj, sec_idx);
|
||||
#endif
|
||||
err = bpf_object__init_internal_map(obj, LIBBPF_MAP_DATA,
|
||||
sec_name, sec_idx,
|
||||
sec_desc->data->d_buf,
|
||||
@ -1600,14 +1703,22 @@ static int bpf_object__init_global_data_maps(struct bpf_object *obj)
|
||||
break;
|
||||
case SEC_RODATA:
|
||||
obj->has_rodata = true;
|
||||
#if defined HAVE_LIBELF
|
||||
sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
|
||||
#elif defined HAVE_ELFIO
|
||||
sec_name = elf_sec_name_by_idx(obj, sec_idx);
|
||||
#endif
|
||||
err = bpf_object__init_internal_map(obj, LIBBPF_MAP_RODATA,
|
||||
sec_name, sec_idx,
|
||||
sec_desc->data->d_buf,
|
||||
sec_desc->data->d_size);
|
||||
break;
|
||||
case SEC_BSS:
|
||||
#if defined HAVE_LIBELF
|
||||
sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, sec_idx));
|
||||
#elif defined HAVE_ELFIO
|
||||
sec_name = elf_sec_name_by_idx(obj, sec_idx);
|
||||
#endif
|
||||
err = bpf_object__init_internal_map(obj, LIBBPF_MAP_BSS,
|
||||
sec_name, sec_idx,
|
||||
NULL,
|
||||
@ -1928,7 +2039,9 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
|
||||
Elf_Data *symbols = obj->efile.symbols;
|
||||
int i, map_def_sz = 0, nr_maps = 0, nr_syms;
|
||||
Elf_Data *data = NULL;
|
||||
#ifdef HAVE_LIBELF
|
||||
Elf_Scn *scn;
|
||||
#endif
|
||||
|
||||
if (obj->efile.maps_shndx < 0)
|
||||
return 0;
|
||||
@ -1941,9 +2054,19 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
|
||||
if (!symbols)
|
||||
return -EINVAL;
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
scn = elf_sec_by_idx(obj, obj->efile.maps_shndx);
|
||||
data = elf_sec_data(obj, scn);
|
||||
#elif defined HAVE_ELFIO
|
||||
Elf_Data realdata;
|
||||
data = elf_sec_data_by_idx(obj, obj->efile.maps_shndx, &realdata);
|
||||
#endif
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
if (!scn || !data) {
|
||||
#elif defined HAVE_ELFIO
|
||||
if (!data) {
|
||||
#endif
|
||||
pr_warn("elf: failed to get legacy map definitions for %s\n",
|
||||
obj->path);
|
||||
return -EINVAL;
|
||||
@ -2552,14 +2675,22 @@ static int bpf_object__init_user_btf_maps(struct bpf_object *obj, bool strict,
|
||||
const struct btf_type *t;
|
||||
const char *name;
|
||||
Elf_Data *data;
|
||||
#ifdef HAVE_LIBELF
|
||||
Elf_Scn *scn;
|
||||
#endif
|
||||
|
||||
if (obj->efile.btf_maps_shndx < 0)
|
||||
return 0;
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
scn = elf_sec_by_idx(obj, obj->efile.btf_maps_shndx);
|
||||
data = elf_sec_data(obj, scn);
|
||||
if (!scn || !data) {
|
||||
#elif defined HAVE_ELFIO
|
||||
Elf_Data realdata;
|
||||
data = elf_sec_data_by_idx(obj, obj->efile.btf_maps_shndx, &realdata);
|
||||
if (!data) {
|
||||
#endif
|
||||
pr_warn("elf: failed to get %s map definitions for %s\n",
|
||||
MAPS_ELF_SEC, obj->path);
|
||||
return -EINVAL;
|
||||
@ -2619,7 +2750,12 @@ static bool section_have_execinstr(struct bpf_object *obj, int idx)
|
||||
{
|
||||
Elf64_Shdr *sh;
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
sh = elf_sec_hdr(obj, elf_sec_by_idx(obj, idx));
|
||||
#elif defined HAVE_ELFIO
|
||||
Elf64_Shdr header;
|
||||
sh = elf_sec_hdr_by_idx(obj, idx, &header);
|
||||
#endif
|
||||
if (!sh)
|
||||
return false;
|
||||
|
||||
@ -3051,7 +3187,11 @@ static const char *elf_sym_str(const struct bpf_object *obj, size_t off)
|
||||
{
|
||||
const char *name;
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
name = elf_strptr(obj->efile.elf, obj->efile.strtabidx, off);
|
||||
#elif defined HAVE_ELFIO
|
||||
name = elfio_string_get_string(obj->efile.strstring, off);
|
||||
#endif
|
||||
if (!name) {
|
||||
pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
|
||||
off, obj->path, elf_errmsg(-1));
|
||||
@ -3065,7 +3205,11 @@ static const char *elf_sec_str(const struct bpf_object *obj, size_t off)
|
||||
{
|
||||
const char *name;
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
name = elf_strptr(obj->efile.elf, obj->efile.shstrndx, off);
|
||||
#elif defined HAVE_ELFIO
|
||||
name = elfio_string_get_string(obj->efile.shstring, off);
|
||||
#endif
|
||||
if (!name) {
|
||||
pr_warn("elf: failed to get section name string at offset %zu from %s: %s\n",
|
||||
off, obj->path, elf_errmsg(-1));
|
||||
@ -3075,6 +3219,7 @@ static const char *elf_sec_str(const struct bpf_object *obj, size_t off)
|
||||
return name;
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
static Elf_Scn *elf_sec_by_idx(const struct bpf_object *obj, size_t idx)
|
||||
{
|
||||
Elf_Scn *scn;
|
||||
@ -3146,6 +3291,44 @@ static const char *elf_sec_name(const struct bpf_object *obj, Elf_Scn *scn)
|
||||
return name;
|
||||
}
|
||||
|
||||
#elif defined HAVE_ELFIO
|
||||
static Elf64_Shdr *elf_sec_hdr_by_idx(const struct bpf_object *obj, size_t idx, Elf64_Shdr *sheader)
|
||||
{
|
||||
psection_t psection = elfio_get_section_by_index(obj->efile.elf, idx);
|
||||
|
||||
sheader->sh_name = elfio_section_get_name_string_offset(psection);
|
||||
sheader->sh_type = elfio_section_get_type(psection);
|
||||
sheader->sh_flags = elfio_section_get_flags(psection);
|
||||
sheader->sh_addr = elfio_section_get_address(psection);
|
||||
sheader->sh_offset = elfio_section_get_offset(psection);
|
||||
sheader->sh_size = elfio_section_get_size(psection);
|
||||
sheader->sh_link = elfio_section_get_link(psection);
|
||||
sheader->sh_info = elfio_section_get_info(psection);
|
||||
sheader->sh_addralign = elfio_section_get_addr_align(psection);
|
||||
sheader->sh_entsize = elfio_section_get_entry_size(psection);
|
||||
|
||||
return sheader;
|
||||
}
|
||||
|
||||
static const char *elf_sec_name_by_idx(const struct bpf_object *obj, size_t idx)
|
||||
{
|
||||
const char *name;
|
||||
Elf64_Shdr sh;
|
||||
|
||||
elf_sec_hdr_by_idx(obj, idx, &sh);
|
||||
|
||||
name = elf_sec_str(obj, sh.sh_name);
|
||||
if (!name) {
|
||||
pr_warn("elf: failed to get section(%zu) name from %s: %s\n",
|
||||
idx, obj->path, elf_errmsg(-1));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn)
|
||||
{
|
||||
Elf_Data *data;
|
||||
@ -3164,6 +3347,28 @@ static Elf_Data *elf_sec_data(const struct bpf_object *obj, Elf_Scn *scn)
|
||||
return data;
|
||||
}
|
||||
|
||||
#elif defined HAVE_ELFIO
|
||||
static Elf_Data *elf_sec_data_by_name(const struct bpf_object *obj, const char *name, Elf_Data *data)
|
||||
{
|
||||
pelfio_t elf = obj->efile.elf;
|
||||
psection_t psection_name = elfio_get_section_by_name(elf, name);
|
||||
data->d_buf = (void*)elfio_section_get_data(psection_name);
|
||||
data->d_size = elfio_section_get_size(psection_name);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
static Elf_Data *elf_sec_data_by_idx(const struct bpf_object *obj, size_t idx, Elf_Data *data)
|
||||
{
|
||||
pelfio_t elf = obj->efile.elf;
|
||||
psection_t psection_index = elfio_get_section_by_index(elf, idx);
|
||||
data->d_buf = (void*)elfio_section_get_data(psection_index);
|
||||
data->d_size = elfio_section_get_size(psection_index);
|
||||
|
||||
return data;
|
||||
}
|
||||
#endif
|
||||
|
||||
static Elf64_Sym *elf_sym_by_idx(const struct bpf_object *obj, size_t idx)
|
||||
{
|
||||
if (idx >= obj->efile.symbols->d_size / sizeof(Elf64_Sym))
|
||||
@ -3235,14 +3440,24 @@ static int cmp_progs(const void *_a, const void *_b)
|
||||
static int bpf_object__elf_collect(struct bpf_object *obj)
|
||||
{
|
||||
struct elf_sec_desc *sec_desc;
|
||||
#if defined HAVE_LIBELF
|
||||
Elf *elf = obj->efile.elf;
|
||||
#elif defined HAVE_ELFIO
|
||||
pelfio_t elf = obj->efile.elf;
|
||||
#endif
|
||||
Elf_Data *btf_ext_data = NULL;
|
||||
Elf_Data *btf_data = NULL;
|
||||
int idx = 0, err = 0;
|
||||
const char *name;
|
||||
Elf_Data *data;
|
||||
#ifdef HAVE_LIBELF
|
||||
Elf_Scn *scn;
|
||||
#endif
|
||||
Elf64_Shdr *sh;
|
||||
#ifdef HAVE_ELFIO
|
||||
Elf64_Shdr secHeader = {0};
|
||||
sh = &secHeader;
|
||||
#endif
|
||||
|
||||
/* ELF section indices are 0-based, but sec #0 is special "invalid"
|
||||
* section. e_shnum does include sec #0, so e_shnum is the necessary
|
||||
@ -3256,9 +3471,16 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
|
||||
/* a bunch of ELF parsing functionality depends on processing symbols,
|
||||
* so do the first pass and find the symbol table
|
||||
*/
|
||||
#if defined HAVE_LIBELF
|
||||
scn = NULL;
|
||||
while ((scn = elf_nextscn(elf, scn)) != NULL) {
|
||||
sh = elf_sec_hdr(obj, scn);
|
||||
#elif defined HAVE_ELFIO
|
||||
int secno = elfio_get_sections_num(elf);
|
||||
for ( int i = 0; i < secno; i++ ) {
|
||||
Elf_Data realdata;
|
||||
sh = elf_sec_hdr_by_idx(obj, i, sh);
|
||||
#endif
|
||||
if (!sh)
|
||||
return -LIBBPF_ERRNO__FORMAT;
|
||||
|
||||
@ -3268,30 +3490,83 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
|
||||
return -LIBBPF_ERRNO__FORMAT;
|
||||
}
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
data = elf_sec_data(obj, scn);
|
||||
#elif defined HAVE_ELFIO
|
||||
data = elf_sec_data_by_idx(obj, i, &realdata);
|
||||
#endif
|
||||
if (!data)
|
||||
return -LIBBPF_ERRNO__FORMAT;
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
idx = elf_ndxscn(scn);
|
||||
#endif
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
obj->efile.symbols = data;
|
||||
#elif defined HAVE_ELFIO
|
||||
obj->efile.realsymbols.d_buf = data->d_buf;
|
||||
obj->efile.realsymbols.d_size = data->d_size;
|
||||
obj->efile.symbols = &(obj->efile.realsymbols);
|
||||
#endif
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
obj->efile.symbols_shndx = idx;
|
||||
#elif defined HAVE_ELFIO
|
||||
obj->efile.symbols_shndx = i;
|
||||
#endif
|
||||
obj->efile.strtabidx = sh->sh_link;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAVE_ELFIO
|
||||
pstring_t shstring;
|
||||
pstring_t strstring;
|
||||
|
||||
psection_t psection = elfio_get_section_by_index(elf, obj->efile.strtabidx);
|
||||
if (!psection)
|
||||
return -LIBBPF_ERRNO__FORMAT;
|
||||
strstring = elfio_string_section_accessor_new(psection);
|
||||
|
||||
psection = elfio_get_section_by_index(elf, obj->efile.shstrndx);
|
||||
if (!psection)
|
||||
return -LIBBPF_ERRNO__FORMAT;
|
||||
shstring = elfio_string_section_accessor_new(psection);
|
||||
|
||||
if (!strstring || !shstring)
|
||||
return -LIBBPF_ERRNO__FORMAT;
|
||||
obj->efile.strstring = strstring;
|
||||
obj->efile.shstring = shstring;
|
||||
#endif
|
||||
|
||||
if (!obj->efile.symbols) {
|
||||
pr_warn("elf: couldn't find symbol table in %s, stripped object file?\n",
|
||||
obj->path);
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
scn = NULL;
|
||||
while ((scn = elf_nextscn(elf, scn)) != NULL) {
|
||||
#elif defined HAVE_ELFIO
|
||||
for ( int i = 0; i < secno; i++ ) {
|
||||
psection_t ptmpsection = elfio_get_section_by_index(elf, i);
|
||||
elf_sec_hdr_by_idx(obj, i, sh);
|
||||
#endif
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
idx = elf_ndxscn(scn);
|
||||
#elif defined HAVE_ELFIO
|
||||
idx = i;
|
||||
#endif
|
||||
sec_desc = &obj->efile.secs[idx];
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
sh = elf_sec_hdr(obj, scn);
|
||||
#elif defined HAVE_ELFIO
|
||||
sh = elf_sec_hdr_by_idx(obj, i, sh);
|
||||
#endif
|
||||
|
||||
if (!sh)
|
||||
return -LIBBPF_ERRNO__FORMAT;
|
||||
|
||||
@ -3302,7 +3577,11 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
|
||||
if (ignore_elf_section(sh, name))
|
||||
continue;
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
data = elf_sec_data(obj, scn);
|
||||
#elif defined HAVE_ELFIO
|
||||
data = elf_sec_data_by_idx(obj, i, &sec_desc->realdata);
|
||||
#endif
|
||||
if (!data)
|
||||
return -LIBBPF_ERRNO__FORMAT;
|
||||
|
||||
@ -3343,15 +3622,37 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
|
||||
} else if (strcmp(name, DATA_SEC) == 0 ||
|
||||
str_has_pfx(name, DATA_SEC ".")) {
|
||||
sec_desc->sec_type = SEC_DATA;
|
||||
#if defined HAVE_LIBELF
|
||||
sec_desc->shdr = sh;
|
||||
sec_desc->data = data;
|
||||
#elif defined HAVE_ELFIO
|
||||
sec_desc->psection = ptmpsection;
|
||||
sec_desc->realdata.d_buf = data->d_buf;
|
||||
sec_desc->realdata.d_size = data->d_size;
|
||||
sec_desc->data = &(sec_desc->realdata);
|
||||
#endif
|
||||
} else if (strcmp(name, RODATA_SEC) == 0 ||
|
||||
str_has_pfx(name, RODATA_SEC ".")) {
|
||||
sec_desc->sec_type = SEC_RODATA;
|
||||
#if defined HAVE_LIBELF
|
||||
sec_desc->shdr = sh;
|
||||
sec_desc->data = data;
|
||||
#elif defined HAVE_ELFIO
|
||||
sec_desc->psection = ptmpsection;
|
||||
sec_desc->realdata.d_buf = data->d_buf;
|
||||
sec_desc->realdata.d_size = data->d_size;
|
||||
sec_desc->data = &(sec_desc->realdata);
|
||||
#endif
|
||||
|
||||
} else if (strcmp(name, STRUCT_OPS_SEC) == 0) {
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
obj->efile.st_ops_data = data;
|
||||
#elif defined HAVE_ELFIO
|
||||
obj->efile.realst_ops_data.d_buf = data->d_buf;
|
||||
obj->efile.realst_ops_data.d_size = data->d_size;
|
||||
obj->efile.st_ops_data = &(obj->efile.realst_ops_data);
|
||||
#endif
|
||||
obj->efile.st_ops_shndx = idx;
|
||||
} else {
|
||||
pr_info("elf: skipping unrecognized data section(%d) %s\n",
|
||||
@ -3368,18 +3669,32 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
|
||||
if (!section_have_execinstr(obj, targ_sec_idx) &&
|
||||
strcmp(name, ".rel" STRUCT_OPS_SEC) &&
|
||||
strcmp(name, ".rel" MAPS_ELF_SEC)) {
|
||||
#if defined HAVE_LIBELF
|
||||
pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n",
|
||||
idx, name, targ_sec_idx,
|
||||
elf_sec_name(obj, elf_sec_by_idx(obj, targ_sec_idx)) ?: "<?>");
|
||||
#elif defined HAVE_ELFIO
|
||||
pr_info("elf: skipping relo section(%d) %s for section(%d) %s\n",
|
||||
idx, name, targ_sec_idx,
|
||||
elf_sec_name_by_idx(obj, targ_sec_idx) ?: "<?>");
|
||||
#endif
|
||||
continue;
|
||||
}
|
||||
|
||||
sec_desc->sec_type = SEC_RELO;
|
||||
#if defined HAVE_LIBELF
|
||||
sec_desc->shdr = sh;
|
||||
#elif defined HAVE_ELFIO
|
||||
sec_desc->psection = ptmpsection;
|
||||
#endif
|
||||
sec_desc->data = data;
|
||||
} else if (sh->sh_type == SHT_NOBITS && strcmp(name, BSS_SEC) == 0) {
|
||||
sec_desc->sec_type = SEC_BSS;
|
||||
#if defined HAVE_LIBELF
|
||||
sec_desc->shdr = sh;
|
||||
#elif defined HAVE_ELFIO
|
||||
sec_desc->psection = ptmpsection;
|
||||
#endif
|
||||
sec_desc->data = data;
|
||||
} else {
|
||||
pr_info("elf: skipping section(%d) %s (size %zu)\n", idx, name,
|
||||
@ -3609,14 +3924,22 @@ static int bpf_object__collect_externs(struct bpf_object *obj)
|
||||
struct extern_desc *ext;
|
||||
int i, n, off, dummy_var_btf_id;
|
||||
const char *ext_name, *sec_name;
|
||||
#ifdef HAVE_LIBELF
|
||||
Elf_Scn *scn;
|
||||
#endif
|
||||
Elf64_Shdr *sh;
|
||||
Elf64_Shdr shheader;
|
||||
|
||||
if (!obj->efile.symbols)
|
||||
return 0;
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
scn = elf_sec_by_idx(obj, obj->efile.symbols_shndx);
|
||||
sh = elf_sec_hdr(obj, scn);
|
||||
#elif defined HAVE_ELFIO
|
||||
sh = &shheader;
|
||||
sh = elf_sec_hdr_by_idx(obj, obj->efile.symbols_shndx, sh);
|
||||
#endif
|
||||
if (!sh || sh->sh_entsize != sizeof(Elf64_Sym))
|
||||
return -LIBBPF_ERRNO__FORMAT;
|
||||
|
||||
@ -3949,7 +4272,11 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
|
||||
}
|
||||
/* text_shndx can be 0, if no default "main" program exists */
|
||||
if (!shdr_idx || shdr_idx != obj->efile.text_shndx) {
|
||||
#if defined HAVE_LIBELF
|
||||
sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
|
||||
#elif defined HAVE_ELFIO
|
||||
sym_sec_name = elf_sec_name_by_idx(obj, shdr_idx);
|
||||
#endif
|
||||
pr_warn("prog '%s': bad call relo against '%s' in section '%s'\n",
|
||||
prog->name, sym_name, sym_sec_name);
|
||||
return -LIBBPF_ERRNO__RELOC;
|
||||
@ -3989,7 +4316,11 @@ static int bpf_program__record_reloc(struct bpf_program *prog,
|
||||
}
|
||||
|
||||
type = bpf_object__section_to_libbpf_map_type(obj, shdr_idx);
|
||||
#if defined HAVE_LIBELF
|
||||
sym_sec_name = elf_sec_name(obj, elf_sec_by_idx(obj, shdr_idx));
|
||||
#elif defined HAVE_ELFIO
|
||||
sym_sec_name = elf_sec_name_by_idx(obj, shdr_idx);
|
||||
#endif
|
||||
|
||||
/* generic map reference relocation */
|
||||
if (type == LIBBPF_MAP_UNSPEC) {
|
||||
@ -4090,7 +4421,9 @@ bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Dat
|
||||
int err, i, nrels;
|
||||
const char *sym_name;
|
||||
__u32 insn_idx;
|
||||
#ifdef HAVE_LIBELF
|
||||
Elf_Scn *scn;
|
||||
#endif
|
||||
Elf_Data *scn_data;
|
||||
Elf64_Sym *sym;
|
||||
Elf64_Rel *rel;
|
||||
@ -4098,6 +4431,7 @@ bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Dat
|
||||
if (sec_idx >= obj->efile.sec_cnt)
|
||||
return -EINVAL;
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
scn = elf_sec_by_idx(obj, sec_idx);
|
||||
scn_data = elf_sec_data(obj, scn);
|
||||
|
||||
@ -4105,6 +4439,15 @@ bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Dat
|
||||
sec_name = elf_sec_name(obj, scn);
|
||||
if (!relo_sec_name || !sec_name)
|
||||
return -EINVAL;
|
||||
#elif defined HAVE_ELFIO
|
||||
Elf_Data realdata;
|
||||
scn_data = elf_sec_data_by_idx(obj, sec_idx, &realdata);
|
||||
|
||||
relo_sec_name = elf_sec_str(obj, shdr->sh_name);
|
||||
sec_name = elf_sec_name_by_idx(obj, sec_idx);
|
||||
if (!relo_sec_name || !sec_name)
|
||||
return -EINVAL;
|
||||
#endif
|
||||
|
||||
pr_debug("sec '%s': collecting relocation for section(%zu) '%s'\n",
|
||||
relo_sec_name, sec_idx, sec_name);
|
||||
@ -4145,7 +4488,11 @@ bpf_object__collect_prog_relos(struct bpf_object *obj, Elf64_Shdr *shdr, Elf_Dat
|
||||
* instead
|
||||
*/
|
||||
if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && sym->st_name == 0)
|
||||
#if defined HAVE_LIBELF
|
||||
sym_name = elf_sec_name(obj, elf_sec_by_idx(obj, sym->st_shndx));
|
||||
#elif defined HAVE_ELFIO
|
||||
sym_name = elf_sec_name_by_idx(obj, sym->st_shndx);
|
||||
#endif
|
||||
else
|
||||
sym_name = elf_sym_str(obj, sym->st_name);
|
||||
sym_name = sym_name ?: "<?";
|
||||
@ -5611,7 +5958,9 @@ bpf_object__relocate_core(struct bpf_object *obj, const char *targ_btf_path)
|
||||
return 0;
|
||||
|
||||
if (targ_btf_path) {
|
||||
#ifdef HAVE_LIBELF
|
||||
obj->btf_vmlinux_override = btf__parse(targ_btf_path, NULL);
|
||||
#endif
|
||||
err = libbpf_get_error(obj->btf_vmlinux_override);
|
||||
if (err) {
|
||||
pr_warn("failed to parse target BTF: %d\n", err);
|
||||
@ -6478,11 +6827,16 @@ static int bpf_object__collect_relos(struct bpf_object *obj)
|
||||
Elf64_Shdr *shdr;
|
||||
Elf_Data *data;
|
||||
int idx;
|
||||
Elf64_Shdr shdrelf;
|
||||
|
||||
if (sec_desc->sec_type != SEC_RELO)
|
||||
continue;
|
||||
|
||||
#if defined HAVE_LIBELF
|
||||
shdr = sec_desc->shdr;
|
||||
#elif defined HAVE_ELFIO
|
||||
shdr = elf_sec_hdr_by_idx(obj, i, &shdrelf);
|
||||
#endif
|
||||
data = sec_desc->data;
|
||||
idx = shdr->sh_info;
|
||||
|
||||
@ -6979,11 +7333,13 @@ static struct bpf_object *bpf_object_open(const char *path, const void *obj_buf,
|
||||
size_t log_size;
|
||||
__u32 log_level;
|
||||
|
||||
#ifdef HAVE_LIBELF
|
||||
if (elf_version(EV_CURRENT) == EV_NONE) {
|
||||
pr_warn("failed to init libelf for %s\n",
|
||||
path ? : "(mem buf)");
|
||||
return ERR_PTR(-LIBBPF_ERRNO__LIBELF);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!OPTS_VALID(opts, bpf_object_open_opts))
|
||||
return ERR_PTR(-EINVAL);
|
||||
|
Loading…
Reference in New Issue
Block a user