Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround linker issue #1

Open
wants to merge 1 commit into
base: defined_in_discarded_section
Choose a base branch
from

Conversation

sehe
Copy link

@sehe sehe commented Feb 8, 2024

from https://stackoverflow.com/questions/77959920/linker-error-defined-in-discarded-section-with-boost-asio-awaitable-operators

So, there are two key components:

  • two separate linker objects (the main program and the library) instantiate a set of template instances
  • the one from the library is discarded because it is unused

Key to reproducing this is that the exact instantiations of deferred async operations match in both the main program and the library implementation details. This, to me, seems like most likely a compiler issue.

I've reworded the main/lib sources to remove a-typical things:

  • the coro lacked a return statement, anyways, so that was invoking UB)
  • non of the parallel-group deferred operations where actually awaited, and I worried that somehow this caused more symbols to be discarded than otherwise, so I added those.
  • File main.cxx

    #include "src/hello.hxx"
    #include <boost/asio.hpp>
    #include <boost/asio/experimental/awaitable_operators.hpp>
    
    int main() {
        namespace asio = boost::asio;
        using asio::ip::tcp;
        using namespace asio::experimental::awaitable_operators;
    
        asio::io_context ioc;
        tcp::socket      s(ioc);
    
        auto token = asio::use_awaitable;
        co_spawn(
            ioc,
            (s.async_connect({}, token) && s.async_connect({}, token)),
            asio::detached);
        abc();
    }
  • File src/hello.cxx

    #include "hello.hxx"
    #include <boost/asio.hpp>
    #include <boost/asio/experimental/awaitable_operators.hpp>
    
    boost::asio::awaitable<void> helloWorld() {
        namespace asio = boost::asio;
        using asio::ip::tcp;
        using namespace asio::experimental::awaitable_operators;
    
        tcp::socket s(co_await asio::this_coro::executor);
        auto token = asio::use_awaitable;
        co_await (s.async_connect({}, token) && s.async_connect({}, token));
    
        co_return;
    }
    
    int abc() { return 42; }

Veryifying the Hypothesis

If you change the token in one of the two translation units to e.g.

auto token = as_tuple(asio::use_awaitable);

the linker is satisfied

Workaround

Like I said, I think this likely is a compiler bug to do with c++20 coroutine machinery. However, since creating your own token does work around it, I'd consider duplicating the implementation:

struct my_token_t : boost::asio::use_awaitable_t<> { } static inline constexpr my_token;
template <typename... Sigs>
struct boost::asio::async_result<my_token_t, Sigs...> : async_result<use_awaitable_t<>, Sigs...> {};

Now the conflict is avoided:

@sehe
Copy link
Author

sehe commented Feb 8, 2024

Oops. I spoke too soon. I accidentally misinterpreted the build output, probably forgot that hello.cxx wasn't saved. It appears that the offending template instantiations are more indirectly related to the token, and more has to be duplicated to hide the issue. I will see whether I can make this happen. Still consider reporting a compiler issue, or if you cannot get traction, report it at https://github.com/chriskohlhoff/asio/issues


WORKDIR /home/conan/cxx_template/build
COPY src /home/conan/cxx_template/src
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merely moved some copies down so we don't always need to redo the conan stuff on simple edits

@sehe sehe mentioned this pull request Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant