]> git.ipfire.org Git - thirdparty/iw.git/commitdiff
iw: fix cmd_size determination with LTO
authorJohannes Berg <johannes.berg@intel.com>
Thu, 28 Feb 2019 20:34:03 +0000 (21:34 +0100)
committerJohannes Berg <johannes.berg@intel.com>
Thu, 28 Feb 2019 20:34:03 +0000 (21:34 +0100)
Use a separate section to determine the cmd_size as in LTO
the entries in __cmd get freely rearranged, leading to a
(usually) very large cmd_size, and thus to the whole thing
not working.

Change-Id: I3437ad34de1d927961a3e98f109794d7a884327f
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
iw.c

diff --git a/iw.c b/iw.c
index 76f152d775d8af83be3b7be6a2356c50c9719214..afc811892188d5dc094df26c9d973ec88b7221e3 100644 (file)
--- a/iw.c
+++ b/iw.c
@@ -542,6 +542,25 @@ int handle_cmd(struct nl80211_state *state, enum id_input idby,
        return __handle_cmd(state, idby, argc, argv, NULL);
 }
 
+/*
+ * Unfortunately, I don't know how densely the linker packs the struct cmd.
+ * For example, if you have a 72-byte struct cmd, the linker will pad each
+ * out to 96 bytes before putting them together in the section. There must
+ * be some algorithm, but I haven't found it yet.
+ *
+ * We used to calculate this by taking the (abs value of) the difference
+ * between __section_get and __section_set, but if LTO is enabled then this
+ * stops working because the entries of the "__cmd" section get rearranged
+ * freely by the compiler/linker.
+ *
+ * Fix this by using yet another "__sizer" section that only contains these
+ * two entries - then the (abs value of) the difference between them will
+ * be how they get packed and that can be used to iterate the __cmd section
+ * as well.
+ */
+static struct cmd sizer1 __attribute__((used,section("__sizer"))) = {};
+static struct cmd sizer2 __attribute__((used,section("__sizer"))) = {};
+
 int main(int argc, char **argv)
 {
        struct nl80211_state nlstate;
@@ -549,7 +568,7 @@ int main(int argc, char **argv)
        const struct cmd *cmd = NULL;
 
        /* calculate command size including padding */
-       cmd_size = labs((long)&__section_set - (long)&__section_get);
+       cmd_size = labs((uintptr_t)&sizer2 - (uintptr_t)&sizer1);
        /* strip off self */
        argc--;
        argv0 = *argv++;