Index: channels/chan_pjsip.c
===================================================================
--- channels/chan_pjsip.c	(revision 424849)
+++ channels/chan_pjsip.c	(working copy)
@@ -2181,6 +2181,8 @@
 {
 	struct ao2_container *endpoints;
 
+	CHECK_PJSIP_SESSION_MODULE_LOADED();
+
 	if (!(chan_pjsip_tech.capabilities = ast_format_cap_alloc(0))) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
Index: include/asterisk/res_pjsip.h
===================================================================
--- include/asterisk/res_pjsip.h	(revision 424849)
+++ include/asterisk/res_pjsip.h	(working copy)
@@ -1949,4 +1949,13 @@
  */
 char *ast_sip_get_debug(void);
 
+/*! \brief Determines whether the res_pjsip module is loaded */
+#define CHECK_PJSIP_MODULE_LOADED()				\
+	do {							\
+		if (!ast_module_check("res_pjsip.so")		\
+			|| !ast_sip_get_pjsip_endpoint()) {	\
+			return AST_MODULE_LOAD_DECLINE;		\
+		}						\
+	} while(0)
+
 #endif /* _RES_PJSIP_H */
Index: include/asterisk/res_pjsip_pubsub.h
===================================================================
--- include/asterisk/res_pjsip_pubsub.h	(revision 424849)
+++ include/asterisk/res_pjsip_pubsub.h	(working copy)
@@ -739,4 +739,13 @@
  */
 const char *ast_sip_subscription_get_body_subtype(struct ast_sip_subscription *sub);
 
+/*! \brief Determines whether the res_pjsip_pubsub module is loaded */
+#define CHECK_PJSIP_PUBSUB_MODULE_LOADED()			\
+	do {							\
+		CHECK_PJSIP_MODULE_LOADED();			\
+		if (!ast_module_check("res_pjsip_pubsub.so")) {	\
+			return AST_MODULE_LOAD_DECLINE;		\
+		}						\
+	} while(0)
+
 #endif /* RES_PJSIP_PUBSUB_H */
Index: include/asterisk/res_pjsip_session.h
===================================================================
--- include/asterisk/res_pjsip_session.h	(revision 424849)
+++ include/asterisk/res_pjsip_session.h	(working copy)
@@ -638,4 +638,13 @@
  */
 void ast_sip_session_resume_reinvite(struct ast_sip_session *session);
 
+/*! \brief Determines whether the res_pjsip_session module is loaded */
+#define CHECK_PJSIP_SESSION_MODULE_LOADED()				\
+	do {								\
+		CHECK_PJSIP_MODULE_LOADED();				\
+		if (!ast_module_check("res_pjsip_session.so")) {	\
+			return AST_MODULE_LOAD_DECLINE;			\
+		}							\
+	} while(0)
+
 #endif /* _RES_PJSIP_SESSION_H */
Index: res/res_hep_pjsip.c
===================================================================
--- res/res_hep_pjsip.c	(revision 424849)
+++ res/res_hep_pjsip.c	(working copy)
@@ -160,6 +160,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_MODULE_LOADED();
+
 	ast_sip_register_service(&logging_module);
 	return AST_MODULE_LOAD_SUCCESS;
 }
Index: res/res_pjsip_acl.c
===================================================================
--- res/res_pjsip_acl.c	(revision 424849)
+++ res/res_pjsip_acl.c	(working copy)
@@ -266,6 +266,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_MODULE_LOADED();
+
 	ast_sorcery_apply_default(ast_sip_get_sorcery(), SIP_SORCERY_ACL_TYPE,
 				  "config", "pjsip.conf,criteria=type=acl");
 
Index: res/res_pjsip_authenticator_digest.c
===================================================================
--- res/res_pjsip_authenticator_digest.c	(revision 424849)
+++ res/res_pjsip_authenticator_digest.c	(working copy)
@@ -461,6 +461,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_MODULE_LOADED();
+
 	if (build_entity_id()) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
Index: res/res_pjsip_caller_id.c
===================================================================
--- res/res_pjsip_caller_id.c	(revision 424849)
+++ res/res_pjsip_caller_id.c	(working copy)
@@ -730,6 +730,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_SESSION_MODULE_LOADED();
+
 	ast_sip_session_register_supplement(&caller_id_supplement);
 	return AST_MODULE_LOAD_SUCCESS;
 }
Index: res/res_pjsip_dialog_info_body_generator.c
===================================================================
--- res/res_pjsip_dialog_info_body_generator.c	(revision 424849)
+++ res/res_pjsip_dialog_info_body_generator.c	(working copy)
@@ -196,6 +196,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_PUBSUB_MODULE_LOADED();
+
 	if (ast_sip_pubsub_register_body_generator(&dialog_info_body_generator)) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
Index: res/res_pjsip_diversion.c
===================================================================
--- res/res_pjsip_diversion.c	(revision 424849)
+++ res/res_pjsip_diversion.c	(working copy)
@@ -330,6 +330,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_SESSION_MODULE_LOADED();
+
 	ast_sip_session_register_supplement(&diversion_supplement);
 	return AST_MODULE_LOAD_SUCCESS;
 }
Index: res/res_pjsip_dtmf_info.c
===================================================================
--- res/res_pjsip_dtmf_info.c	(revision 424849)
+++ res/res_pjsip_dtmf_info.c	(working copy)
@@ -150,6 +150,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_SESSION_MODULE_LOADED();
+
 	ast_sip_session_register_supplement(&dtmf_info_supplement);
 	return AST_MODULE_LOAD_SUCCESS;
 }
Index: res/res_pjsip_endpoint_identifier_anonymous.c
===================================================================
--- res/res_pjsip_endpoint_identifier_anonymous.c	(revision 424849)
+++ res/res_pjsip_endpoint_identifier_anonymous.c	(working copy)
@@ -108,6 +108,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_MODULE_LOADED();
+
 	ast_sip_register_endpoint_identifier(&anonymous_identifier);
 	return AST_MODULE_LOAD_SUCCESS;
 }
Index: res/res_pjsip_endpoint_identifier_ip.c
===================================================================
--- res/res_pjsip_endpoint_identifier_ip.c	(revision 424849)
+++ res/res_pjsip_endpoint_identifier_ip.c	(working copy)
@@ -410,6 +410,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_MODULE_LOADED();
+
 	ast_sorcery_apply_config(ast_sip_get_sorcery(), "res_pjsip_endpoint_identifier_ip");
 	ast_sorcery_apply_default(ast_sip_get_sorcery(), "identify", "config", "pjsip.conf,criteria=type=identify");
 
Index: res/res_pjsip_endpoint_identifier_user.c
===================================================================
--- res/res_pjsip_endpoint_identifier_user.c	(revision 424849)
+++ res/res_pjsip_endpoint_identifier_user.c	(working copy)
@@ -114,6 +114,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_MODULE_LOADED();
+
 	ast_sip_register_endpoint_identifier(&username_identifier);
 	return AST_MODULE_LOAD_SUCCESS;
 }
Index: res/res_pjsip_exten_state.c
===================================================================
--- res/res_pjsip_exten_state.c	(revision 424849)
+++ res/res_pjsip_exten_state.c	(working copy)
@@ -528,6 +528,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_MODULE_LOADED();
+
 	if (ast_sip_register_subscription_handler(&presence_handler)) {
 		ast_log(LOG_WARNING, "Unable to register subscription handler %s\n",
 			presence_handler.event_name);
Index: res/res_pjsip_header_funcs.c
===================================================================
--- res/res_pjsip_header_funcs.c	(revision 424849)
+++ res/res_pjsip_header_funcs.c	(working copy)
@@ -604,6 +604,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_SESSION_MODULE_LOADED();
+
 	ast_sip_session_register_supplement(&header_funcs_supplement);
 	ast_custom_function_register(&pjsip_header_function);
 
Index: res/res_pjsip_logger.c
===================================================================
--- res/res_pjsip_logger.c	(revision 424849)
+++ res/res_pjsip_logger.c	(working copy)
@@ -233,6 +233,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_MODULE_LOADED();
+
 	if (ast_sorcery_observer_add(ast_sip_get_sorcery(), "global", &global_observer)) {
 		ast_log(LOG_WARNING, "Unable to add global observer\n");
 		return AST_MODULE_LOAD_DECLINE;
Index: res/res_pjsip_messaging.c
===================================================================
--- res/res_pjsip_messaging.c	(revision 424849)
+++ res/res_pjsip_messaging.c	(working copy)
@@ -723,6 +723,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_SESSION_MODULE_LOADED();
+
 	if (ast_sip_register_service(&messaging_module) != PJ_SUCCESS) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
Index: res/res_pjsip_multihomed.c
===================================================================
--- res/res_pjsip_multihomed.c	(revision 424849)
+++ res/res_pjsip_multihomed.c	(working copy)
@@ -201,6 +201,8 @@
 {
 	pj_sockaddr addr;
 
+	CHECK_PJSIP_MODULE_LOADED();
+
 	if (!pj_gethostip(pj_AF_INET(), &addr)) {
 		pj_sockaddr_print(&addr, host_ipv4, sizeof(host_ipv4), 2);
 	}
Index: res/res_pjsip_mwi.c
===================================================================
--- res/res_pjsip_mwi.c	(revision 424849)
+++ res/res_pjsip_mwi.c	(working copy)
@@ -958,6 +958,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_MODULE_LOADED();
+
 	if (ast_sip_register_subscription_handler(&mwi_handler)) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
Index: res/res_pjsip_mwi_body_generator.c
===================================================================
--- res/res_pjsip_mwi_body_generator.c	(revision 424849)
+++ res/res_pjsip_mwi_body_generator.c	(working copy)
@@ -94,6 +94,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_PUBSUB_MODULE_LOADED();
+
 	if (ast_sip_pubsub_register_body_generator(&mwi_generator)) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
Index: res/res_pjsip_nat.c
===================================================================
--- res/res_pjsip_nat.c	(revision 424849)
+++ res/res_pjsip_nat.c	(working copy)
@@ -281,6 +281,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_SESSION_MODULE_LOADED();
+
 	if (ast_sip_register_service(&nat_module)) {
 		ast_log(LOG_ERROR, "Could not register NAT module for incoming and outgoing requests\n");
 		return AST_MODULE_LOAD_FAILURE;
Index: res/res_pjsip_notify.c
===================================================================
--- res/res_pjsip_notify.c	(revision 424849)
+++ res/res_pjsip_notify.c	(working copy)
@@ -750,6 +750,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_MODULE_LOADED();
+
 	if (aco_info_init(&notify_cfg)) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
Index: res/res_pjsip_one_touch_record_info.c
===================================================================
--- res/res_pjsip_one_touch_record_info.c	(revision 424849)
+++ res/res_pjsip_one_touch_record_info.c	(working copy)
@@ -107,6 +107,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_SESSION_MODULE_LOADED();
+
 	if (ast_sip_session_register_supplement(&info_supplement)) {
 		ast_log(LOG_ERROR, "Unable to register One Touch Recording supplement\n");
 		return AST_MODULE_LOAD_FAILURE;
Index: res/res_pjsip_outbound_authenticator_digest.c
===================================================================
--- res/res_pjsip_outbound_authenticator_digest.c	(revision 424849)
+++ res/res_pjsip_outbound_authenticator_digest.c	(working copy)
@@ -145,6 +145,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_MODULE_LOADED();
+
 	if (ast_sip_register_outbound_authenticator(&digest_authenticator)) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
Index: res/res_pjsip_outbound_registration.c
===================================================================
--- res/res_pjsip_outbound_registration.c	(revision 424849)
+++ res/res_pjsip_outbound_registration.c	(working copy)
@@ -1241,6 +1241,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_MODULE_LOADED();
+
 	ast_sorcery_apply_default(ast_sip_get_sorcery(), "registration", "config", "pjsip.conf,criteria=type=registration");
 
 	if (ast_sorcery_object_register(ast_sip_get_sorcery(), "registration", sip_outbound_registration_alloc, NULL, sip_outbound_registration_apply)) {
Index: res/res_pjsip_path.c
===================================================================
--- res/res_pjsip_path.c	(revision 424849)
+++ res/res_pjsip_path.c	(working copy)
@@ -224,6 +224,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_SESSION_MODULE_LOADED();
+
 	if (ast_sip_register_supplement(&path_supplement)) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
Index: res/res_pjsip_pidf_body_generator.c
===================================================================
--- res/res_pjsip_pidf_body_generator.c	(revision 424849)
+++ res/res_pjsip_pidf_body_generator.c	(working copy)
@@ -118,6 +118,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_PUBSUB_MODULE_LOADED();
+
 	if (ast_sip_pubsub_register_body_generator(&pidf_body_generator)) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
Index: res/res_pjsip_pidf_digium_body_supplement.c
===================================================================
--- res/res_pjsip_pidf_digium_body_supplement.c	(revision 424849)
+++ res/res_pjsip_pidf_digium_body_supplement.c	(working copy)
@@ -95,6 +95,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_PUBSUB_MODULE_LOADED();
+
 	if (ast_sip_pubsub_register_body_supplement(&pidf_supplement)) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
Index: res/res_pjsip_pidf_eyebeam_body_supplement.c
===================================================================
--- res/res_pjsip_pidf_eyebeam_body_supplement.c	(revision 424849)
+++ res/res_pjsip_pidf_eyebeam_body_supplement.c	(working copy)
@@ -94,6 +94,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_PUBSUB_MODULE_LOADED();
+
 	if (ast_sip_pubsub_register_body_supplement(&pidf_supplement)) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
Index: res/res_pjsip_pubsub.c
===================================================================
--- res/res_pjsip_pubsub.c	(revision 424849)
+++ res/res_pjsip_pubsub.c	(working copy)
@@ -1867,8 +1867,12 @@
 static int load_module(void)
 {
 	static const pj_str_t str_PUBLISH = { "PUBLISH", 7 };
-	struct ast_sorcery *sorcery = ast_sip_get_sorcery();
+	struct ast_sorcery *sorcery;
 
+	CHECK_PJSIP_MODULE_LOADED();
+
+	sorcery = ast_sip_get_sorcery();
+
 	pjsip_evsub_init_module(ast_sip_get_pjsip_endpoint());
 
 	if (!(sched = ast_sched_context_create())) {
Index: res/res_pjsip_refer.c
===================================================================
--- res/res_pjsip_refer.c	(revision 424849)
+++ res/res_pjsip_refer.c	(working copy)
@@ -20,7 +20,6 @@
 	<depend>pjproject</depend>
 	<depend>res_pjsip</depend>
 	<depend>res_pjsip_session</depend>
-	<depend>res_pjsip_pubsub</depend>
 	<support_level>core</support_level>
  ***/
 
@@ -985,6 +984,8 @@
 {
 	const pj_str_t str_norefersub = { "norefersub", 10 };
 
+	CHECK_PJSIP_SESSION_MODULE_LOADED();
+
 	pjsip_replaces_init_module(ast_sip_get_pjsip_endpoint());
 	pjsip_xfer_init_module(ast_sip_get_pjsip_endpoint());
 	pjsip_endpt_add_capability(ast_sip_get_pjsip_endpoint(), NULL, PJSIP_H_SUPPORTED, NULL, 1, &str_norefersub);
Index: res/res_pjsip_registrar.c
===================================================================
--- res/res_pjsip_registrar.c	(revision 424849)
+++ res/res_pjsip_registrar.c	(working copy)
@@ -793,6 +793,8 @@
 {
 	const pj_str_t STR_REGISTER = { "REGISTER", 8 };
 
+	CHECK_PJSIP_MODULE_LOADED();
+
 	if (!(serializers = ao2_container_alloc(
 		      SERIALIZER_BUCKETS, serializer_hash, serializer_cmp))) {
 		return AST_MODULE_LOAD_DECLINE;
Index: res/res_pjsip_registrar_expire.c
===================================================================
--- res/res_pjsip_registrar_expire.c	(revision 424849)
+++ res/res_pjsip_registrar_expire.c	(working copy)
@@ -177,6 +177,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_MODULE_LOADED();
+
 	if (!(contact_autoexpire = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, CONTACT_AUTOEXPIRE_BUCKETS,
 		contact_expiration_hash, contact_expiration_cmp))) {
 		ast_log(LOG_ERROR, "Could not create container for contact auto-expiration\n");
Index: res/res_pjsip_rfc3326.c
===================================================================
--- res/res_pjsip_rfc3326.c	(revision 424849)
+++ res/res_pjsip_rfc3326.c	(working copy)
@@ -130,6 +130,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_SESSION_MODULE_LOADED();
+
 	ast_sip_session_register_supplement(&rfc3326_supplement);
 	return AST_MODULE_LOAD_SUCCESS;
 }
Index: res/res_pjsip_sdp_rtp.c
===================================================================
--- res/res_pjsip_sdp_rtp.c	(revision 424849)
+++ res/res_pjsip_sdp_rtp.c	(working copy)
@@ -1254,6 +1254,8 @@
  */
 static int load_module(void)
 {
+	CHECK_PJSIP_SESSION_MODULE_LOADED();
+
 	ast_sockaddr_parse(&address_ipv4, "0.0.0.0", 0);
 	ast_sockaddr_parse(&address_ipv6, "::", 0);
 
Index: res/res_pjsip_send_to_voicemail.c
===================================================================
--- res/res_pjsip_send_to_voicemail.c	(revision 424849)
+++ res/res_pjsip_send_to_voicemail.c	(working copy)
@@ -207,6 +207,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_SESSION_MODULE_LOADED();
+
 	if (ast_sip_session_register_supplement(&refer_supplement)) {
 		ast_log(LOG_ERROR, "Unable to register Send to Voicemail supplement\n");
 		return AST_MODULE_LOAD_FAILURE;
Index: res/res_pjsip_session.c
===================================================================
--- res/res_pjsip_session.c	(revision 424849)
+++ res/res_pjsip_session.c	(working copy)
@@ -2405,6 +2405,8 @@
 {
 	pjsip_endpoint *endpt;
 
+	CHECK_PJSIP_MODULE_LOADED();
+
 	if (!ast_sip_get_sorcery() || !ast_sip_get_pjsip_endpoint()) {
 		return AST_MODULE_LOAD_DECLINE;
 	}
Index: res/res_pjsip_t38.c
===================================================================
--- res/res_pjsip_t38.c	(revision 424849)
+++ res/res_pjsip_t38.c	(working copy)
@@ -850,6 +850,8 @@
  */
 static int load_module(void)
 {
+	CHECK_PJSIP_SESSION_MODULE_LOADED();
+
 	ast_sockaddr_parse(&address_ipv4, "0.0.0.0", 0);
 	ast_sockaddr_parse(&address_ipv6, "::", 0);
 
Index: res/res_pjsip_transport_websocket.c
===================================================================
--- res/res_pjsip_transport_websocket.c	(revision 424849)
+++ res/res_pjsip_transport_websocket.c	(working copy)
@@ -361,6 +361,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_MODULE_LOADED();
+
 	pjsip_transport_register_type(PJSIP_TRANSPORT_RELIABLE, "WS", 5060, &transport_type_ws);
 	pjsip_transport_register_type(PJSIP_TRANSPORT_RELIABLE, "WSS", 5060, &transport_type_wss);
 
Index: res/res_pjsip_xpidf_body_generator.c
===================================================================
--- res/res_pjsip_xpidf_body_generator.c	(revision 424849)
+++ res/res_pjsip_xpidf_body_generator.c	(working copy)
@@ -151,6 +151,8 @@
 
 static int load_module(void)
 {
+	CHECK_PJSIP_PUBSUB_MODULE_LOADED();
+
 	if (ast_sip_pubsub_register_body_generator(&xpidf_body_generator)) {
 		goto fail;
 	}
